์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[์ธํ”„๋Ÿฐ ์„น์…˜8] ํŒฉํ† ๋ฆฌ์–ผ (Javascript)

์€์ง„ 2021. 6. 29. 17:39

์ธํ”„๋Ÿฐ

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[์ธํ”„๋Ÿฐ ์„น์…˜8] ํŒฉํ† ๋ฆฌ์–ผ (Javascript)
์œ ๋ฃŒ ๊ฐ•์˜์ธ ๊ด€๊ณ„๋กœ ๋ฌธ์ œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.


โœ๏ธIdea Sketch

2021-06-29

1. ํŒฉํ† ๋ฆฌ์–ผ ์ถœ๋ ฅํ•˜๊ธฐ

2. for ๋ฐ˜๋ณต๋ฌธ, ์ดˆ๊ธฐ๊ฐ’ 1

3. ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ์ข…๋ฃŒ์กฐ๊ฑด if (v > n) return;
  • ๋‚ด๋ถ€๋กœ์ง res *= v;


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-29 ๋ฐ˜๋ณต๋ฌธ

function factorial(n) {
  let res = 1;

  for (let i = 1; i <= n; i++) {
    res *= i;
  }

  return res;
}

console.log(factorial(5))


2021-06-29 ์žฌ๊ท€

let n = 5;
let res = 1;

function DFS(v) {
  if (v > n) return;
  res *= v;
  DFS(v + 1);
}

DFS(1);
console.log(res);