+ 1

Usuage of Recursion vs while vs for loop(which one's faster?)

function pw_with_while(base, exponent){ let i = 1; var c = 1; while( i <= exponent){ c=c*base i++ } return c } function pw_with_for(base, exponent){ var c = 1; for(let i = 1; i <= exponent; i++){ c=c*base; } return c; } function pw_with_recursion(base, exponent){ if(exponent===0){ return 1; }else{ return base * pw_with_recursion(base, exponent-1); } } document.write("power with while (10,2): " + pw_with_while(10,2)+"<br>"); document.write("power with for (10,2): " + pw_with_for(10,2)+"<br>"); document.write("power with recursion (10,2): " + pw_with_recursion(10,2));

24th Aug 2018, 10:42 AM
AFSAR JAN
AFSAR JAN - avatar
1 Odpowiedź
24th Aug 2018, 10:53 AM
Sudarshan Rai
Sudarshan Rai - avatar