0

i don't get what´s wrong with my code :(

I just wrote this simple code and it always gets one time more function getPower(num1, num2) { for (i = num1, x = 1; x <= num2 ; i *= num1, x++); return i } getPower(5, 2); //The output would be 125 here To fix that, i just set x=2 and it's working fine, but can someone explain why it doesnt work with x=1?

19th Jan 2017, 10:09 PM
Losel Matos
Losel Matos - avatar
1 Answer
+ 1
You set i to num1, so you basically already did the first iteration of the loop. You can set it to i = 1 if you want to start at x = 1, or you change the x <= num2 to x < num2 and start at x = 1 or you simply start at x = 2, all three of these solutions should work.
19th Jan 2017, 10:34 PM
Robobrine
Robobrine - avatar