what is wrong? `def taker(n) mul = [] (1..n).each do |i| mul << i if (i % 15).zero? end return mul.sum end puts taker(10)` in ruby

The code you provided has a syntax error. There are two issues with the code:

  1. The method definition is missing parentheses after the method name. It should be def taker(n) instead of def taker n.

  2. The sum method does not exist for an array in Ruby. You can replace mul.sum with mul.inject(:+) to find the sum of elements in the array.

Here's the corrected code:

main.rb
def taker(n)
  mul = []
  (1..n).each do |i|
    mul << i if (i % 15).zero?
  end
  return mul.inject(:+)
end

puts taker(10)
126 chars
10 lines

This code will output the sum of all numbers from 1 to n that are divisible by both 3 and 5.

related categories

gistlibby LogSnag