1

courses, credits and points are Ruby arrays with an equal size.

gpa = (([credits, courses, points].transpose.map {|x| x.reduce(:*)}).inject{|sum,x| sum + x }).round(2)

This method prompted an error message when I was trying to run it.

Error message:

in 'each': undefined method '*' for nil:NilClass (NoMethodError)
sponavitus
  • 35
  • 1
  • 6

1 Answers1

1

From the error message, it seems that some of the data is nil.

Perhaps you can attempt to force the data into floats.

It's not ideal, as you might end up with silent errors, but it's worth a shot.

try:

[credits, courses, points].transpose.map {|a| a.map {|n| n.to_f} } .map { |x| x.reduce(:*) } .inject{|sum, x| sum + x} .round(2)

It works for me even with strings and weird data (although the incompatible data is simply converted to zeros, effecting the math).

Myst
  • 18,516
  • 2
  • 45
  • 67