본문 바로가기
코테/코테withJS

백준 Node.js(자바스크립트) 2446문제 별찍기 -9

by 트레일헤드레인저 2021. 9. 20.
SMALL

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = +fs.readFileSync(filePath).toString();

star(input);

function star(num) {
  for (let i = 0; i < num * 2 - 1; i++) {
    
    let stars = "";
    if (i < num) {

      for (let j = 0; j < i; j++) {
        stars += " ";
      }
      for (let k = 0; k < (num - i) * 2 - 1; k++) {
        stars += "*";
      }
      console.log(stars);
    } else {
      for (let j=0; j<2*(num-1)-i; j++) {
        stars += " ";
      }
      for(let j=0; j<(i+1-num)*2+1; j++) {
        stars += "*";
      }

      console.log(stars);
    }

  }
}

코드에서 어려운 부분은 없었으나 오래걸렸다 난 하수다

LIST