문제

 

풀이 코드

process.stdin.setEncoding('utf8');
process.stdin.on('data', (data) => {
  const n = data.split(' ');
  const a = Number(n[0]),
    b = Number(n[1]);
  writeStar(a, b);
});

function writeStar(a, b) {
  let starCount = '';
  for (let i = 0; i < a; i++) {
    starCount += '*';
  }

  for (let i = 0; i < b; i++) {
    console.log(starCount);
  }
}

 

풀이 과정

starCount 라는 변수에 for 반복문을 사용하여 * 을 a 입력만큼 저장 하였고,

b 만큼 starCount 를 출력했다.

 

풀면서도 너무 비효율적인거 같아 고민하다가 다른사람의 풀이를 보았다.

 

다른사람의 풀이

process.stdin.setEncoding('utf8');
process.stdin.on('data', data => {
    const n = data.split(" ");
    const a = Number(n[0]), b = Number(n[1]);
    console.log((('*').repeat(a)+`\n`).repeat(b))
});

 

기초가 중요한 이유가 여기있다

 

for 를 남발하지 않고도 간략하게 repeat 함수를 이용하여 구현 할 수 있는 문제였다.

 

오늘은 repeat 함수에 대하여 공부해 봐야겠다!

+ Recent posts