logo CodeStepByStep logo

exponent

Language/Type: JavaScript recursion
Author: Melissa Hovik (on 2016/09/25)

Write a recursive function named exponent that accepts two integers representing a base and an exponent and returns the base raised to that exponent. For example, the call of exponent(3, 4) should return 34 or 81. If a negative exponent is passed, the function should throw an error with the message, "exponent should be non-negative".

Constraints:

  • Do not use loops or auxiliary data structures; solve the problem recursively.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.