Countdown Recursion
Published in
Oct 1, 2020
Q. Write an algorithm that counts down with recursion.
const countdown = function (n) { if (n === 0) return; console.log(n); countdown(n - 1);}countdown(10);// should be 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- If
n === 0
, return nothing. - Output
n
. - Use
countdown
function and set the parameter(n-1)
.
n = 10? n === 0 // False
console.log(n) // 10
countdown(9)n = 9? n === 0 // False
console.log(n) // 9
countdown(8)...n = 1? n === 1 // False
console.log(n) // 1
countdown(0)n = 0? n === 0 // True