Zack
Zack

Reputation: 671

.times block not working. How come?

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

Answers (1)

user1726343
user1726343

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

Related Questions