문제 :
Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.
function FirstFactorial(num) {
let sum = 1;
for (i = num; i > 0; i--) {
sum = sum * i;
}
return sum;
}
// keep this function call here
console.log(FirstFactorial(readline()));
쉽다고 생각하고 제출했는데 모법답안 보고 이렇게 짰어야 했다.. 싶고
재귀함수 쓰는게 아직 어색하다고 해야하나
사실 제대로 못 쓰고 있는 것같다
function FirstFactorial(num) {
return (num === 1 ? 1 : num * FirstFactorial(num - 1));
}
'Algorithm' 카테고리의 다른 글
[LeetCode]Remove Duplicates from Sorted Array (Splice, Set 활용) (0) | 2023.08.31 |
---|---|
[coderbyte/js] First Reverse (0) | 2022.07.07 |
[프로그래머스/js] 하샤드 수 (reduce를 사용해 값을 누적하기) (0) | 2022.07.07 |
[프로그래머스/js] 음양더하기 (reduce 사용해서 배열의 합구하기) (0) | 2022.07.06 |
[프로그래머스/js] 숫자 문자열과 영단어 (0) | 2022.07.06 |