I heard recursive function is powerful so instead going through loop i tried to create a function which increments a number until it reach certain points. when it reaches i trie
rec
will either return i
(if i
is over 100) or undefined
(otherwise).
When you call it here:
console.log(rec());
i
will be 1
so it will return undefined
.
You never do anything with the return value when the return value is over 100.
You need to return the result of the recursive call:
} else {
return rec();
}
so when it is over 100, the value gets passed back up the stack.
it this kind of recursion is good then for loop?
No. It is horribly inefficient.