const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : './data.txt';
const input = Number(fs.readFileSync(path).toString().trim());
const solve = (input) => {
if (input === 1 || input === 0) { // input <= 1 ๋ก๋ ๊ฐ๋ฅ
return 1;
} else {
return input * solve(input - 1);
}
};
console.log(solve(input));
์๋ฐ์คํฌ๋ฆฝํธ๋ก ํผ ์ฒซ ์ฌ๊ทํจ์ ๋ฌธ์ !
ํญ์ ์ฌ๊ท๋ฅผ ํ ๋, ์ฌ๊ท๋ฅผ ๋ฉ์ถ๊ฒ ํ๋ base case๋ฅผ ๋ ๊ธฐ์ตํ์.
์ฆ, ์ฌ๊ท์ ์ผ๋ก ๊ทธ ๋ค์๋จ๊ณ๋ฅผ returnํ๋ ๋ถ๋ถ์ด ์๋ค๋ฉด ๋ฐ๋์ ์ฌ๊ท๋ฅผ ๋ฉ์ถ๋ ๋ถ๋ถ๋ ์์ด์ผํ๋ค.
'๐ JavaScript > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] 2798 (0) | 2022.06.07 |
---|---|
[JS] 10870 (0) | 2022.05.28 |
[JS] 9020 (0) | 2022.05.23 |
[JS] 4948 (0) | 2022.05.23 |
[JS] 1929 (0) | 2022.05.22 |