function recursion(x) {
if (x < 0) return
console.log(x)
recursion(x - 1)
}
recursion(5);
I am getting the following output
5
4
3
2
1
0 => i want to know why 0, isn’t the condition will be false at this point
function recursion(x) {
if (x < 0) return
console.log(x)
recursion(x - 1)
}
recursion(5);
I am getting the following output
5
4
3
2
1
0 => i want to know why 0, isn’t the condition will be false at this point
3
Answers
You have a wrong condition in the
if
, fix:The condition you putted that is if(x = 5 < 0) that means it will iterates untill the "x" value is not less then "0". so according to your condition it takes to the 0 .
I hopes you find the answer.
There are a few ways to fix this:
<=
)0
, or<
)1
Here is the iterative version:
Could also be written as: