Snippet:
function loop(x) {
if (x >= 10) {
console.log(x); // adding this line changes x value to 10
return x; // returning x on the next line results in undefined output
}
loop(x + 1);
}
console.log(loop(0));
The current output is "undefined" instead of 10.
I am seeking a comprehensive explanation on how recursion functions and tips on fixing the bug present in my code. Any suggestions?