+ 2

Why does the following code outputs 6? [JS]

function addRek( x ) { if( x == 0 ) { return 0; } else { return x + addRek( x - 1 ); } } console.log( addRek(3) ); Thank you. :).

9th Mar 2017, 1:40 PM
Sharky6
Sharky6 - avatar
5 Respuestas
+ 6
This program recursively calls addRek till your passed value isn't reduced to zero. so output is 3+2+1=6
9th Mar 2017, 1:42 PM
Megatron
Megatron - avatar
+ 5
Sorry for my explanation but does it really outputs 6
10th Mar 2017, 3:23 AM
Megatron
Megatron - avatar
0
dude. I don't get it
9th Mar 2017, 5:22 PM
Sharky6
Sharky6 - avatar
0
heyo. thanks for the nice response man. But I didn't get it haha. I will probably ask a friend one on one so he could explain it to me. Thank you again ; )
10th Mar 2017, 7:01 AM
Sharky6
Sharky6 - avatar
0
yeah it really does outputs 6 haha
10th Mar 2017, 7:01 AM
Sharky6
Sharky6 - avatar