JavaScript Competitive Scripting (Part 2)

Make your own power function.

In a time when we thought robots would be wipe our asses by now it’s quite disappointing to see the real picture .But as you know everyone must learn so here’s my next installment of doing something that the computer should be doing for you but still it’s nice to learn once in a while .

Method 1: Recursively add a to get the multiplication of two numbers.
 And recursively multiply to get a raise to the power b.
function iveGotThepower(a,b){
    return multiply(a,iveGotThepower(a,b-1));
  return 1;
//Home made multiply function without the * operator
function multiply(a,b){
    return 0;
  return a+multiply(a,b-1);
//Log out the output
Method 2:Divide and Conquer.
Cut the problem in half each time.Follow the code you will get it easily.
function gopower(x,y){
  if(y==0)return 1;
  var temp=gopower(x,parseInt(y/2));
  if(y%2==0)return temp*temp;
    return x*temp*temp;
var a =gopower(2,3);

Note: The last method is far better.

You can check and test the code on your browser by right clicking anywhere on the browser screen and the click on inspect element .

Click on console tab (the reason for the lat line of code ).Just Copy paste the whole thing .



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s