power

Medium · Mathematical Calculations · 3 test cases

Given a base and an exponent, return the base to the power of the exponent. You can assume the exponent is a non-negative integer. Do not use the `Math.pow()` function or the `**` operator.

Examples

power(2, 3) → 8
power(5, 0) → 1
power(3, 4) → 81

Starter code

function power(base, exp) {

}

Complexity of the optimal solution

Time O(n), space O(1). This function uses a loop that runs 'exp' (let's call it n) times. The number of multiplications is directly proportional to the exponent. Therefore, the time complexity is O(n), with constant space.

Solve power in the browser