+ 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));
1 Resposta