ํ๋ก๊ทธ๋๋จธ์ค - ๋ฌธ์์ด ๋๋ฆฌ๊ธฐ - 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
'Study_Develop > ์๊ณ ๋ฆฌ์ฆ | ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค - ๋ฌธ์์ด ๊ฒน์ณ์ฐ๊ธฐ - js (splice ์ฌ์ฉ๋ฒ) (0) | 2023.07.24 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค - ํ์ง ๊ตฌ๋ถํ๊ธฐ - js (JavaScript ` ` (๋ฐฑํฑ๊ธฐํธ)) (0) | 2023.07.24 |
ํ๋ก๊ทธ๋๋จธ์ค - ๋ฌธ์์ด ๋ถ์ฌ์ ์ถ๋ ฅํ๊ธฐ - js (0) | 2023.07.23 |
ํ๋ก๊ทธ๋๋จธ์ค - ๋ง์ ์ ์ถ๋ ฅํ๊ธฐ - JS (0) | 2023.07.23 |
ํ๋ก๊ทธ๋๋จธ์ค - ํน์ ๋ฌธ์ ์ถ๋ ฅํ๊ธฐ - JS (0) | 2023.07.23 |