ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ฌธ์ž์—ด ๋Œ๋ฆฌ๊ธฐ - js (forEach๋ฌธ, for ... of ๋ฃจํ”„)

2023. 7. 24. 16:15ใ†Study_Develop/์•Œ๊ณ ๋ฆฌ์ฆ˜ | ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

๋ฐ˜์‘ํ˜•

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ฌธ์ž์—ด ๋Œ๋ฆฌ๊ธฐ - js

 

๋ฌธ์ œ

๋ฌธ์ž์—ด str์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด์„ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ 90๋„ ๋Œ๋ ค์„œ ์•„๋ž˜ ์ž…์ถœ๋ ฅ ์˜ˆ์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

1 ≤ str์˜ ๊ธธ์ด ≤ 10

์ž…๋ ฅ

abcde

 

์ถœ๋ ฅ

a
b
c
d
e

 

solution.js

ํ•ด๊ฒฐ๋ฐฉ๋ฒ• 1๋ฒˆ>

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line
}).on('close',function(){
    [...input].forEach(a => console.log(a))
});

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ• 2๋ฒˆ>

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line;
}).on('close', function () {
    for (const char of input) {
        console.log(char);
    }
});

 

forEach๋ฌธ

 forEach()๋Š” ์ฃผ์–ด์ง„ callback์„ ๋ฐฐ์—ด์— ์žˆ๋Š” ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์‚ญ์ œํ–ˆ๊ฑฐ๋‚˜ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์€ ์ธ๋ฑ์Šค ์†์„ฑ์— ๋Œ€ํ•ด์„œ๋Š” ์‹คํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (์˜ˆ: ํฌ์†Œ ๋ฐฐ์—ด)

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// Expected output: "a"
// Expected output: "b"
// Expected output: "c"

 

for ... of ๋ฃจํ”„

 

for ... of๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž์—ด์„ ํ™•์žฅํ•  ํ•„์š” ์—†์ด input๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋ฅผ ์ง์ ‘๋ฐ˜๋ณตํ• ์ˆ˜ ์žˆ๋‹ค.

 

const array1 = ['a', 'b', 'c'];

for (const element of array1) {
  console.log(element);
}

// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
let iterable = [10, 20, 30];

for (let value of iterable) {
  console.log(value);
}
// 10
// 20
// 30