Skip to content
Exercise · Functions · lesson 9 of 10

Exercise: power by recursion

Write a recursive function function power(base, exp) that returns base raised to the power exp. The ** operator or a loop would also give the answer, but the point of the task is to practice recursion.

  • power(2, 10) → 1024
  • power(-2, 3) → -8
  • power(7, 0) → 1

Constraints: exp is an integer from 0 to 15, base is an integer from -10 to 10 and may be negative or zero; any number to the power 0 is 1, including power(0, 0).

Do not add extra imports unless the task says so explicitly.

power.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way