Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kmandreza/4414495 to your computer and use it in GitHub Desktop.
Save kmandreza/4414495 to your computer and use it in GitHub Desktop.
Create a method get_grade that accepts an Array of test scores. Each score in the array should be between 0 and 100, where 100 is the max score. Compute the average score and return the letter grade as a String, i.e., 'A', 'B', 'C', 'D', 'E', or 'F'. For example, # How studious! get_grade([100, 100, 100]) # => 'A'
def get_grade(test_scores)
x = test_scores.inject{|sum,x| sum + x } #sum of all test scores
y = x/test_scores.length #divide by number of test scores
if y >= 90
return "A"
elsif y >=80
return "B"
elsif y>= 70
return "C"
elsif y>=60
return "D"
elsif y <60
return "F"
end
end
test_scores = [70, 65, 99, 83]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment