The goal is to check if it is one off. If, for instance, the winning ticket is 1234, and my ticket is 1233, or 2234, or 1334 that should pass as true, as the ticket is one off. Non regex involved please as this was for beginners.
If it is exact, then it fails. If it is more than one number off, it fails. This is what I have so far.
Here is my function:
require "minitest/autorun"
require_relative "offbyone.rb"
class TestLottoFunction < Minitest::Test
def test_a_matching_ticket_returns_false
my_ticket = "1234"
winning_tickets = "1234"
assert_equal(false, off(my_ticket, winning_tickets))
end
def test_a_one_off_ticket
my_ticket = "1234"
winning_tickets = "1235"
assert_equal(true, off(my_ticket, winning_tickets))
end
end
I tried to spell it out in a more complicated way of what my goal was exactly, seeing if that would work, and it didn't. I got the same error message, telling me it is line 9 and 15, telling me that the function is expecting either true or false, and it is receiving nil. This is the code that I have.
def off(my_ticket, winning_tickets)
i = 0
c = 0
4.times do
if winning_tickets[0][i] == my_ticket[0][i]
c+=1
end
i+=1
end
if c==3
true
end
end
I just changed
if winning_tickets[i] == my_tickets [i]
to include
winning_tickets[0][i] == my_tickets [0][i]
and it got rid of one error, but created a new one.
If I don't include the [0][i], I get an error message saying that I have 2 errors, both on the assert_equal lines, both saying they are receiving nil instead of true or false.
If I add the [0][i] to both lines, I get 1 error, saying my error was in the matching ticket, saying it was expecting true and received false.
Any help with this?
I am actually curious as to why I am receiving nil. Where in the array am I going wrong to get nil. Please point out my mistakes.