-
[프로그래머스/JavaScript] 문자열 반복해서 출력하기IT Study/프로그래머스 2023. 10. 10. 18:20728x90
자바스크립트(NodeJS)에서 이제 입력을 받아 출력하는 것은 아주 ⭐️ 조금 ⭐️ 익숙해진 것 같습니다.
위 문제에 접근해보겠습니다.
초기 코드
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let input = []; rl.on('line', function (line) { input = line.split(' '); }).on('close', function () { str = input[0]; n = Number(input[1]); });
중간 과정 (실패 1)
줄바꿈이 추가되지 않아 str이 그대로 이어져서 출력됩니다.
따라서 string\nstring\nstring\nstring\nstring과 같이 출력됩니다.
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let input = []; rl.on('line', function (line) { input = line.split(' '); }).on('close', function () { str = input[0]; n = Number(input[1]); for(let i = 0; i < n; i++) { console.log(str); } }); // string // string // string // string // string
중간 과정 (실패 2)
이 코드에서 result를 초기화하지 않으면 undefined가 할당되기 때문에
undefinedstringstringstringstringstring과 같은 결과가 나옵니다.
따라서 초기값을 let result = '';와 같이 설정해주어야 합니다. (아깝네요...)
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let input = []; rl.on('line', function (line) { input = line.split(' '); }).on('close', function () { str = input[0]; n = Number(input[1]); let result; for(let i = 0; i < n; i++) { result += str; } console.log(result); // undefinedstringstringstringstringstring });
최종 코드
테스트 1 〉통과 (53.08ms, 32.3MB)
테스트 2 〉통과 (47.15ms, 32.3MB)
테스트 3 〉통과 (44.65ms, 32.3MB)const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let input = []; rl.on('line', function (line) { input = line.split(' '); }).on('close', function () { str = input[0]; n = Number(input[1]); let result = str; for(let i = 1; i < n; i++) { result += str; } console.log(result); // stringstringstringstringstring });
+ 중간 과정 (실패 2) 에서 let result = ""; 로 설정했다면 테스트가 얼마나 소요될까요?
테스트 1 〉통과 (65.67ms, 32.4MB)
테스트 2 〉통과 (49.87ms, 32.3MB)
테스트 3 〉통과 (49.64ms, 32.3MB)
저의 최종 코드보다 조금 더 시간이 소요되네요... (ㅎㅎ)+ "좋아요 수가 34인" 다른 사람의 풀이로 풀어본다면 테스트가 얼마나 소요될까요?
console.log(str.repeat(n));
테스트 1 〉통과 (45.81ms, 32.3MB)
테스트 2 〉통과 (44.92ms, 32.3MB)
테스트 3 〉통과 (48.34ms, 32.3MB)
저의 최종 코드보다 조금 더 적게 시간이 소요되네요... (ㅠㅠ)
그렇지만 repeat이라는 새로운 메서드를 알게 되었습니다.
string.repeat(count); // string: 반복할 문자열 // count: 반복 횟수를 나타내는 정수
+ "좋아요 수가 2인" 다른 사람의 풀이도 풀어본다면 테스트가 얼마나 소요될까요?
for (i=0; i<n; i++){ process.stdout.write(str); }
테스트 1 〉통과 (48.17ms, 32.3MB)
테스트 2 〉통과 (44.71ms, 32.3MB)
테스트 3 〉통과 (44.13ms, 32.3MB)
저의 최종 코드보다 조금 더 적게 시간이 소요되네요... (ㅠㅠ)
그렇지만 역시 process.stdout.write이라는 새로운 메서드를 알게 되었습니다.
`\n` 개행 문자를 자동으로 추가하지 않는 메서드라고 하네요.'IT Study > 프로그래머스' 카테고리의 다른 글
[프로그래머스/JavaScript] 주사위 게임 3 (Feat. Map) (0) 2023.10.11 [프로그래머스/JavaScript] 조건 문자열 (Feat. eval) (0) 2023.10.10 [프로그래머스/JavaScript] 문자열 겹쳐쓰기 (0) 2023.10.10 [프로그래머스/JavaScript] 대소문자 바꿔서 출력하기 (0) 2023.10.10 [프로그래머스/JavaScript] 문자열 출력하기 (Feat. 기초 트레이닝) (0) 2023.10.10