ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] JavaScript 알고리즘 | Lv.0 문자열의 뒤의 n글자
    ► JS Algorithm/Programmers 2023. 12. 1. 20:23
    반응형

    🔒 문제 설명

    문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string의 뒤의 n글자로 이루어진 문자열을 return 하는 solution 함수를 작성해 주세요.

     

     


    🔒 제한사항

    • my_string은 숫자와 알파벳으로 이루어져 있습니다.
    • 1 ≤ my_string의 길이 ≤ 1,000
    • 1 ≤ n ≤ my_string 길이

     


    🔒 입출력 예

    my_string n result
    "ProgrammerS123" 11 "grammerS123"
    "He110W0r1d" 5 "W0r1d"

     


    🔒 입출력 예 설명

    입출력 예 #1

    • 예제 1번의 my_string에서 뒤의 11글자는 "grammerS123"이므로 이 문자열을 return 합니다.

    입출력 예 #2

    • 예제 2번의 my_string에서 뒤의 5글자는 "W0r1d"이므로 문자열을 return 합니다.

     


     

    🔐 solution of mine

    shift()

    join('')

          function solution(my_string, n) {
            let num = my_string.length - n;              // 단어총길이 - output에 필요한길이
            let answer = [...my_string];                 // 단어를 배열화함 → num 갯수만큼 shift()함수활용하여, 배열의 앞인덱스부터 지울예정
            for (let i = 0; i < num; i++) {              // num 갯수만큼 for문 회전하며, shift()함수로, 배열 앞인덱스를 삭제함
              answer.shift();           
            }
            return console.log(answer.join(""));         // join('')함수로, 배열을 한단어로 묶음
          }
          
          solution("ProgrammerS123", 11);                // expected output: "grammerS123"
          solution("He110W0r1d", 5);                     // expected output: "W0r1d"

     

     


     

    🔐 solution of others 

    slice()

          function solution(my_string, n) {
            return console.log(my_string.slice(my_string.length - n));
          }
    
          solution("ProgrammerS123", 11);	// expected output: "grammerS123"
          solution("He110W0r1d", 5); 	// expected output: "W0r1d"

     

    🔐 solution of others 

    slice()

          function solution(my_string, n) {
            return console.log(my_string.slice(-n));
         // return console.log(my_string.slice(-1 * n));	// -n 또는 -1 * n 모두 결과는 같다
          }
    
          solution("ProgrammerS123", 11); 	// expected output: "grammerS123"
          solution("He110W0r1d", 5); 	// expected output: "W0r1d"

     

    마이너스 산술

    1 * 2 = 2 
    -1 * 2 = -2
    -1 * -2 = 2

     

    ◆ slice() 구조: slice(begin, end)

     - Begin: 잘라낼 배열 시작 index

     - End: 잘라낼 배열의 종료 index (종료index 잘라낼배열에 미포함됨)

    ◆ slice() 코드예시:

    const nums = ['1', '2', '3', '4', '5'];
    
    console.log(nums.slice(2));		// Expected output: Array ["3", "4", "5"]
    2번index부터 끝까지 출력
    
    console.log(nums.slice(2, 4));		// Expected output: Array ["3", "4"]
    2번index부터 4번인덱스 앞까지 출력
    
    console.log(nums.slice(-2));		// Expected output: Array ["4", "5"]
    Begin의 index가 음수면 배열뒤에서 길이를 나타냄
    배열 뒤부터 2개 출력
    
    console.log(nums.slice(2, -1));		// Expected output: Array ["3", “4”]
    end의 index가 음수면 배열뒤에서 길이의 앞까지를 나타냄
    배열 2번index부터 배열뒤에서 1개 앞까지 출력
    
    console.log(nums.slice());		// Expected output: Array ["1", "2", "3", "4", "5"]
    모두출력

     


     

    반응형
Designed by Tistory.