I just come up with little strange behavior in Javascript Recursion. My code
var temp1 = function(maxLength,accNumber) {
if(accNumber.length < maxLength)
{
accNumber = '0'+accNumber;
temp1(maxLength,accNumber);
}
return accNumber;
};
console.log(temp1(5,"23"));
So Here I am expecting output as "00023" but its giving "023" which is not expected.
But same code with while loop I am getting expected output. I am not understanding why.
var temp1 = function(maxLength,accNumber) {
while(accNumber.length < maxLength)
{
accNumber = '0'+accNumber;
temp1(maxLength,accNumber);
}
return accNumber;
};
console.log(temp1(5,"23"));
Which give me output "00023" as expected.