Reputation: 671
The line adding the exponentiation to result doesn't seem to be doing the trick. How come?
def pow(base, exponent)
result = 0
exponent.times do
result += base * base
end
result
end
Upvotes: 1
Views: 83
Reputation:
The times block is working fine. In order to raise a base b to an exponent n, you need to multiply 1 by b n times.
def pow(base, exponent)
result = 1
exponent.times do
result *= base
end
result
end
Upvotes: 2