Skip to content

Instantly share code, notes, and snippets.

@kh0p
Last active August 29, 2015 13:58
Show Gist options
  • Save kh0p/10003156 to your computer and use it in GitHub Desktop.
Save kh0p/10003156 to your computer and use it in GitHub Desktop.
`NoMethodError: undefined method `+' for nil:NilClass` why?
def scoreThrows(radiuses)
mapd = Array.new
radiuses.each do |x|
mapd << 10 if (x < 5) && (x >= 0)
mapd << 5 if (x <= 10) && (x >= 5)
mapd << 0 if (x > 10)
end
out = mapd.reduce(:+)
radiuses.all? { |x| x <= 5 == true } ? out += 100 : out
end
@kh0p
Copy link
Author

kh0p commented Apr 6, 2014

"You've just recently been hired to calculate scores for a Dart Board game!

Scoring specifications:
0 points - radius above 10
5 points - radius between 5 and 10 inclusive
10 points - radius less than 5

If all radiuses are less than 5, award 100 BONUS POINTS!

Write a function that accepts an array of radiuses (can be integers and/or floats),
and returns a total score using the above specification.
An empty array should return 0.

Examples:
scoreThrows( [1, 5, 11] ) => returns 15
scoreThrows( [15, 20, 30] ) => returns 0
scoreThrows( [1, 2, 3, 4] ) => returns 140" - http://www.codewars.com/dojo/katas/525dfedb5b62f6954d000006/train/ruby

Test.expect(scoreThrows([0, 5, 10, 10.5, 4.5]) == 30, "Expected 30")
Test.expect(scoreThrows([1, 5, 11]) == 15, "Expected 15")
Test.expect(scoreThrows([15, 20, 30]) == 0, "Expected 0")
Test.expect(scoreThrows([1, 2, 3, 4]) == 140, "Expected 140")

^ Every test: passed.

Test.expect(scoreThrows([1, 2, 3, 4, nil]) == 140, "Expected 140")

^ NoMethodError: undefined method '<' for nil:NilClass

So how am I getting NoMethodError: undefined method '+' for nil:NilClass, and how should I handle it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment