+ 1

[JS] Can someone explain the output?

Given is the following function from a challenge: function mlt(a, b) { if(!(a && b)) { return 0; } else if(b == 1) { return a;} else{ return (a + mlt(a, b-1)); } } console.log(mlt(3, 11)); // returns 33 console.log(mlt(7, 1)); // returns 7 I understand the second output (7), but not the first one (33), since it is nested. I have no idea, how on earth it results 33. Would somebody be so kind and explain it?

10th Oct 2017, 1:24 PM
Pete Wright
Pete Wright - avatar
2 odpowiedzi
0
Yeah, in the end it's 3+3, but that equals 6 in common math. It looks like it is treated as string "3"+"3" = "33"
10th Oct 2017, 1:45 PM
Pete Wright
Pete Wright - avatar
0
Thank you for your explanation! Now I get it 😊
12th Oct 2017, 12:14 PM
Pete Wright
Pete Wright - avatar