// find elements
var button = $("button")
// handle click and add class
button.on("click", () => {
$("#divOutput").hide();
var number = parseInt($("#inpNumber").val());
var power = parseInt($("#inpPower").val());
var recOut = powUsingRec(number, power);
var addPow = powUsingAddition(number, power);
$("#spanRecPower").text(recOut);
$("#spanAddPower").text(addPow);
$("#divOutput").show();
})
// A recursive function to get a^b
// Works only if a >= 0 and b >= 0
function powUsingRec(a, b)
{
if (b > 0)
return multiply(a, powUsingRec(a, b - 1));
else
return 1;
}
// A recursive function to get x*y
function multiply(x, y)
{
if (y > 0)
return (x + multiply(x, y - 1));
else
return 0;
}
/* Works only if a >= 0 and b >= 0 */
function powUsingAddition(a , b)
{
if (b == 0)
return 1;
var answer = a;
var increment = a;
var i, j;
for (i = 1; i < b; i++)
{
for (j = 1; j < a; j++)
{
answer += increment;
}
increment = answer;
}
return answer;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.