I want to understand this below recursion. Here times-1
is passed each time function is called. How it reaches to zero? I mean, when times-1
is compared by if(times===1)
then the statments terminates and else is not executed. And how each time decremented value is passed.
function repeatStringNumTimes(string, times) {
if(times < 0)
return "";
if(times === 1)
return string;
else
return string + repeatStringNumTimes(string, times - 1);
}
repeatStringNumTimes("abc", 3);
Aucun commentaire:
Enregistrer un commentaire