I have run in to a slight hiccup. I'm sure it's simple but for the life of me can't figure it out.
The function of the method is simple, given an array of "numbers", return true if any 3 consecutive numbers add up to 7, else false.
My code below satisfies the first condition in full, as in any qualifying set of numbers will return true. My problem is that when an array of numbers do not meet the parameters set in the code, instead of false, I get an error of `+': nil can't be coerced into Fixnum (TypeError), any feed back is helpful. Thanks in advance. Please see code below: * is the problem line.
def lucky_sevens?(numbers)
i = 0
while i < numbers.length
each_number = numbers[i]
next_number = numbers[i+1]
third_number = numbers[i+2]
**if (each_number + next_number + third_number) == 7**
return true
end
i += 1
end
end
You are trying to access
numbers[i+2]. But what happens whenireaches the last element in numbers? TryWhat basically is happening, is that you are accessing an element out of bounds. Also, you should probably put a
return falsestatement after the loop.