Skip to content

Instantly share code, notes, and snippets.

@abdulateef
Created September 12, 2017 11:34
Show Gist options
  • Save abdulateef/6de952fd771997f12ce7265141e1106f to your computer and use it in GitHub Desktop.
Save abdulateef/6de952fd771997f12ce7265141e1106f to your computer and use it in GitHub Desktop.
Write a function named power that accepts two arguments, a and b and calculates a raised to the power b.
def power(a,b):
if b == 0:
return 1
else:
return eval(((str(a)+"*")*b)[:-1])
@samkenny
Copy link

please can u explain the above code, or can i have same code in javascript

@BruceTushabe
Copy link

it brings an error when excuted

@seunjosh
Copy link

seunjosh commented Sep 9, 2018

This should work,for JavaScript

function power(a, b){
var result = 1;

if (b > 0)
{
    for (var i = 1; i <= b; ++i)
    {
        result *= a;
    }
}
else if (b < 0)
{
    for (var i = -1; i >= b; --i)
    {
        result /= a;
    }
}

return result;

}

@teezyfortune
Copy link

teezyfortune commented Mar 21, 2019

please could you please explain this code in lem man language, trying to digest the logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment