Operator and Ruby

0

I would like this code to take two numbers and display the divisible numbers for the first, the second, and the two. But I was not able to make use of the current AND for this case, because the array div_pelos2 returns empty.

def self.divisiveis(num1, num2)
    div_num1 = Array.new
    div_num2 = Array.new
    div_pelos2 = Array.new

    for i in 1..49
        i += 1
        if i % num1 == 0
            div_num1.push (i)
        elsif i % num2 == 0
            div_num2.push (i)
        elsif (i % num1 == 0) && (i % num2 == 0) 
            div_pelos2.push (i)
        end
    end

    return div_pelos2, div_num1, div_num2
    end
end
    
asked by anonymous 12.10.2017 / 21:11

1 answer

2

Your & amp; is correct, the problem is in the elsif. Notice this section:

 elsif (i % num1 == 0) && (i % num2 == 0) 

"elsif" is the same as "else if", so this code snippet is only executed if the condition fails in the other 2 previous scenarios, which test exactly whether the number is divisible by one or the other

Switch to if ... end

    
13.10.2017 / 01:26