Write a recursive function powerx n that returns the value o
Write a recursive function power(x, n) that returns the value of x^n.
 (assume that n is an integer)
 
 Start by writing the base case.
 
 Once implemented, uncomment the relevant displayPower() to see how the result is computed, and uncomment the relevant Program.assertEqual() to make sure the test passes.
var isEven = function(n) {
 return n % 2 === 0;
 };
var isOdd = function(n) {
 return !isEven(n);
 };
var power = function(x, n) {
 println(\"Computing \" + x + \" raised to power \" + n + \".\");
 // base case
 // recursive case: n is negative
 // recursive case: n is odd
 // recursive case: n is even
 };
var displayPower = function(x, n) {
 println(x + \" to the \" + n + \" is \" + power(x, n));
 };
//displayPower(3, 0);
 //Program.assertEqual(power(3, 0), 1);
 //displayPower(3, 1);
 //Program.assertEqual(power(3, 1), 3);
 //displayPower(3, 2);
 //Program.assertEqual(power(3, 2), 9);
 //displayPower(3, -1);
 //Program.assertEqual(power(3, -1), 1/3);
Solution
var power = function(base, exponent){
    return exponent == 0 ? 1 : base * power(base, --exponent);
 }
 var displayPower = function(x, n) {
 document.write(x + \" to the \" + n + \" is \" + power(x, n));
 };
 displayPower(3, 2);

