Error when summing variable and number inside brackets NoMethodError

I don’t know why the error mentioned below happens. It is caused because I use a variable and summing it with a number inside the brackets. How can I fix it?
Any help would be greatly appreciated. I have spent much time searching for the answer but I couldn’t find it. Thank so much!

The Code is this:

def two_sum(nums)
  i = 0
  while i < nums.length
    b = i + 1
    while b <= nums.length
      if nums[i] + nums[b] == 0
        puts("we need to store this numbers")
        puts("It doesn't match")
      i += 1

# These are tests to check that your code is working. After writing
# your solution, they should all print true.

  'two_sum([1, 3, 5, -3]) == [1, 3]: ' + (two_sum([1, 3, 5, -3]) == [1, 3]).to_s
  'two_sum([1, 3, 5]) == nil: ' + (two_sum([1, 3, 5]) == nil).to_s


08-two-sum.rb:06:in `two_sum': undefined method `+' for nil:NilClass (NoMethodError) 
from 08-two-sum.rb:23:in `<main>'

Source: syntax

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.