ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] JavaScript 알고리즘 | Lv.0 잘라서 배열로 저장하기
    ► JS Algorithm/Programmers 2024. 8. 7. 20:59
    반응형

    🔒 문제 설명

    문자열 my_str n 매개변수로 주어질 , my_str 길이 n 잘라서 저장한 배열을 return하도록 solution 함수를 완성해주세요.


    🔒 제한사항 

    • 1 ≤ my_str의 길이 ≤ 100
    • 1 ≤ n ≤ my_str의 길이
    • my_str 알파벳 소문자, 대문자, 숫자로 이루어져 있습니다.

    🔒 입출력 예

    my_str n result
    "abc1Addfggg4556b" 6 ["abc1Ad", "dfggg4", "556b"]
    "abcdef123" 3 ["abc", "def", "123"]

    🔒 입출력 예 설명

    입출력 예 #1

    • "abc1Addfggg4556b" 를 길이 6씩 잘라 배열에 저장한 ["abc1Ad", "dfggg4", "556b"]를 return해야 합니다.

    입출력 예 #2

    • "abcdef123" 를 길이 3씩 잘라 배열에 저장한 ["abc", "def", "123"]를 return해야 합니다.

     

    유의사항

    • 입출력 #1 경우 "abc1Addfggg4556b" 길이 6 자르면 "abc1Ad", "dfggg4" 두개와 마지막 "556b" 남습니다. 이런 경우 남은 문자열을 그대로 배열에 저장합니다.

     

    🔐 solution of mine

    Array.prototype.push()

    Array.prototype.splice()

    Array.prototype.join()

      function solution(my_str, n) {
        const arr = [...my_str];
        const answer = [];
        while (arr.length) {
          answer.push(arr.splice(0, n).join(""));
        }
        return console.log(answer);
      }
    
      solution("abc1Addfggg4556b", 6); // output: ["abc1Ad", "dfggg4", "556b"]
      solution("abcdef123", 3); // output: ["abc", "def", "123"]

     


     

    🔐 solution of others

    Array.prototype.push()

    String.prototype.substring()

      function solution(my_str, n) {
        let answer = [];
        for (let i = 0; i < my_str.length; i += n) {
          answer.push(my_str.substring(i, i + n));
        }
        return console.log(answer);
      }
      
      solution("abc1Addfggg4556b", 6); // output: ["abc1Ad", "dfggg4", "556b"]
      solution("abcdef123", 3); // output: ["abc", "def", "123"]

     

    🔐 solution of others

    Array.prototype.push()

    String.prototype.substr()

      function solution(my_str, n) {
        var answer = [];
        for (let i = 0; i < my_str.length; i += n) {
          answer.push(my_str.substr(i, n));
        }
        return console.log(answer);
      }
    
      solution("abc1Addfggg4556b", 6); // output: ["abc1Ad", "dfggg4", "556b"]
      solution("abcdef123", 3); // output: ["abc", "def", "123"]

    🔐 solution of others

    Array.prototype.push()

    Array.prototype.slice()

      function solution(my_str, n) {
        let answer = [];
        for (let i = 0; i < my_str.length; i += n) {
          answer.push(my_str.slice(i, i + n));
        }
        return console.log(answer);
      }
    
      solution("abc1Addfggg4556b", 6); // output: ["abc1Ad", "dfggg4", "556b"]
      solution("abcdef123", 3); // output: ["abc", "def", "123"]

     

    🔐 solution of others

    Array() 생성자

    Math.ceil()

    Array.prototype.fill()

    Array.prototype.map()

    String.prototype.slice()

      function solution(my_str, n) {
        return console.log(
          Array(Math.ceil(my_str.length / n))
            .fill("")
            .map((_, i) => my_str.slice(i * n, i * n + n))
        );
      }

     

    🔐 solution of others

    Array() 생성자

    Array.from()

    Math.ceil()

    String.prototype.slice()

      function solution(my_str, n) {
        return console.log(
          Array.from({ length: Math.ceil(my_str.length / n) }, (_, i) =>
            my_str.slice(i * n, i * n + n)
          )
        );
      }
    
      solution("abc1Addfggg4556b", 6); // output: ["abc1Ad", "dfggg4", "556b"]
      solution("abcdef123", 3); // output: ["abc", "def", "123"]

     

    🔐 solution of others

    String.prototype.match()

    RegExp

      function solution(my_str, n) {
        return console.log(my_str.match(new RegExp(`.{1,${n}}`, "g")));
      }
    
      solution("abc1Addfggg4556b", 6); // output: ["abc1Ad", "dfggg4", "556b"]
      solution("abcdef123", 3); // output: ["abc", "def", "123"]

     

     해설집 - 배열 비구조화 할당 / 값 교환

    기호 의미
    . 모든 문자열(숫자, 한글, 영어, 특수기호, 공백 모두)
    단, 줄바꿈 X
    {min,max} 최소 Min개 이상, 최대 Max개 이하
    {3,5}? == {3}와 동일
    g 문자열 내의 모든 패턴을 검색한다.

     


     

    반응형
Designed by Tistory.