0

I want to do the sums of multiples of 3 or 5 untill 1000 but I got a problems of unexpected end but I dont understand why

def multiple_of_3_or_5(nb)
    if nb % 3 == 0 || nb % 5 == 0
        return true
    else
        return false
    end
end

def perform
    i = 1
    res = 0
    while i <= 1000
        if multiple_of_3_or_5(i)
            res += i
        end
        i++
    end
    puts res
end

perform

I got those errors in iTerm :

multiples.rb:17: syntax error, unexpected `end'

multiples.rb:21: syntax error, unexpected end-of-input, expecting `end'

But when I remove the end l.17 I got this

multiples.rb:18: syntax error, unexpected local variable or method, expecting `do' or '{' or '(' puts res

Can anyone explain my why and how to do for this programs works?

ffouquet42
  • 124
  • 13
  • 1
    Does this answer your question? [No increment operator (++) in Ruby?](https://stackoverflow.com/questions/3717519/no-increment-operator-in-ruby) – pjs Nov 17 '20 at 16:18
  • By the way, your first method can be reduced to `def multiple_of_3_or_5(nb)`, `nb % 3 == 0 || nb % 5 == 0`, `end` – pjs Nov 17 '20 at 16:21
  • ...and all the logic in `perform` can be replaced by `puts (1..1000).map { |i| multiple_of_3_or_5(i) ? i : 0 }.sum`. – pjs Nov 17 '20 at 16:37

1 Answers1

1

Ok I thought in ruby I can do i++ but i have to i += 1. That's work now.

ffouquet42
  • 124
  • 13