I can not understand why it turns out 8? How does the program work? What are you doing? Into a stupor enters the 3rd line return 1; Why won't just the number 1 return?)

function power(base, exponent) { if (exponent == 0) return 1; else return base * power(base, exponent - 1); } console.log(power(2, 3)); 

    1 answer 1

    Any number in the zero degree, with the exception of zero, is one . Therefore, if exponent == 0 (this is a degree in your cases), then the function returns 1, but otherwise it raises the base number to the exponent degree.

    PS But programmers don't need math :)

    PPS About recursion well told here https://learn.javascript.ru/recursion

    • Thank you =) Now it’s clear) - Oleg Artyukh
    • Glad to help) - Only Me