Problem 7

Compute Exponent Recursion Challenge

Given a base number and an exponent, return the result of raising the base to the given power. The exponent may be negative.

Function Signature

exponent(base, exp)

Parameters

  • base — a non-negative integer representing the base.
  • exp — an integer representing the exponent. May be positive, negative, or zero.

Output

Return a number representing base raised to the power of exp.

Constraints

  • The function must use recursion.
  • Do not use Math.pow or the ** operator.
  • The function should accept exactly two arguments.

Examples

exponent(3, 4)  → 81
exponent(7, 2)  → 49
exponent(8, 0)  → 1
exponent(9, 1)  → 9
exponent(4, -2) → 0.0625
exponent(2, -5) → 0.03125

Edge Cases

  • Any base raised to the power of 0 returns 1.
  • A negative exponent returns the reciprocal (e.g. base-exp = 1 / baseexp).