Skip to content

Instantly share code, notes, and snippets.

@MrBean83
Last active December 20, 2015 01:09
Show Gist options
  • Save MrBean83/6046772 to your computer and use it in GitHub Desktop.
Save MrBean83/6046772 to your computer and use it in GitHub Desktop.
def get_grade(grade)
average = grade.to_i
case average
when 90..100
"A"
when 80...90
"B"
when 70...80
"C"
when 60...70
"D"
else
"F"
end
end
@zdavy
Copy link

zdavy commented Jul 22, 2013

def get_grade(array)
total = 0
x = array.length - 1

// You can use this is so you don't have to determine the number of grades in the array
while x >= 0
total += array[x]
x -=1
end

average = total.to_f / array.length

case average
when 90..100
puts "A"
when 80...90
puts "B"
when 70...80
puts "C"
when 60...70
puts "D"
when 0...60
puts "F"
else
puts "Error"
end

end

@zdavy
Copy link

zdavy commented Jul 22, 2013

def get_grade(array)

// this is how to do it with the inject method
average = array.inject(0) { |sum, num| sum += num }.to_f / array.length

case average
when 90..100
puts"A"
when 80...90
puts"B"
when 70...80
puts"C"
when 60...70
puts"D"
when 0...60
puts"F"
else
puts"Error"
end

end

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