whatisthis?

๋ฐฑ์ค€ 2741 javascript (node.js) ํ’€์ด ๋ณธ๋ฌธ

ALGORITHM/BOJ (Node.js)

๋ฐฑ์ค€ 2741 javascript (node.js) ํ’€์ด

thisisyjin 2021. 12. 11. 21:47

for๋ฌธ - (5)

 

 


 

๐Ÿ’ก๋ฌธ์ œ

์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, 1๋ถ€ํ„ฐ N๊นŒ์ง€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ“์ž…๋ ฅ

์ฒซ์งธ ์ค„์— 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค.

๐Ÿ“ˆ์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๋ฒˆ์งธ ์ค„ ๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ

5

์˜ˆ์ œ ์ถœ๋ ฅ

1
2
3
4
5

 


< ์ฝ”๋“œ >

let input = Number(require('fs').readFileSync('/dev/stdin').toString());
let result = '';

for(let i=1;i<=input;i++) {
   result += i + "\n";
}

console.log(result);

 

 

result ๋ณ€์ˆ˜ (string)๋ฅผ ๊ณต๋ฐฑ('')์œผ๋กœ ์„ ์–ธํ•œ ํ›„ +์—ฐ์‚ฐ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋”ํ•œ๋‹ค.

์ค„๋ฐ”๊ฟˆ์€ '\n' (๊ฐœํ–‰๋ฌธ์ž) ๋กœ ํ•˜๊ณ , ๋‹ค ๋”ํ•œ result์„ ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅํ•˜์—ฌ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๋ฐฉ์ง€.

 

 

 

์ฒ˜์Œ์— ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ (์‹œ๊ฐ„์ดˆ๊ณผ)๐Ÿ”ป

let input = require('fs').readFileSync('/dev/stdin');

let N = Number(input);

for(let i=1;i<=N;i++) {
   console.log(i);
}

- for๋ฌธ ์•ˆ์— console.log๋ฅผ ํ•˜๋ฉด ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•œ๋‹ค.