var power = function(base,exponent){
if(exponent === 0){
return 1;
}
else{
return base * power(base, exponent - 1);
}
};
power(2,2);What does exponent - 1 do in the sixth line of the code?
And what does this mean?
To compute the power of a number base, we multiply it by itself raised to the exponent - 1.
Also, how does this bit of code work in finding the power and how is it different from the method used above?
var power = function (base, exponent) {
var result = 1;
for ( i = 0; i < exponent; i++) {
result = result * base;
}
return result;
};
power(2, 4);What does the for loop do here? Please explain to me.
I'd love help. So please do help me. Any help would be appreciated a lot. Thank you.


Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Back to top








