Last active
December 26, 2015 03:08
-
-
Save tundal45/7083368 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_grade(array) | |
sum = 0 | |
array.each do |x| | |
sum += x | |
end | |
average = sum / array.length | |
case average | |
when 90...100 | |
grade = 'A' | |
when 80...90 | |
grade = 'B' | |
when 70...80 | |
grade = 'C' | |
when 60...70 | |
grade = 'D' | |
when 50...60 | |
grade = 'E' | |
when 0...50 | |
grade = 'F' | |
else | |
"Error" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_grade(hash) # replace array with a hash as a parameter | |
hash = {A: 90..100, B: 80...90, C: 70...80, D: 60...70, E: 50...60, F: 0...50} # create a hash that pairs keys (letter grade) with values (numerical range). Can you use "..." as a value in a hash? | |
sum = 0 # default the sum to 0 | |
hash.each do |key, val| # iterate and say that in the hash, for each value, you're going to add that value to the sum | |
sum += val # incrimenting the value to the sum | |
end | |
average = sum / val.length # defining average, which is the sum divided by the value length (val.length) | |
# not sure what to do here! | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
Submitted the code in the gist below and it works: | |
Now I have to create a hash per the new exercise: | |
If you implemented the chosen challenge with an Array, is it possible to implement with a Hash? Or vice versa? Follow the same objectives below, and answer the questions in bold in your gist. | |
def get_grade(hash) # replace array with a hash as a parameter | |
hash = {A: 90..100, B: 80...90, C: 70...80, D: 60...70, E: 50...60, F: 0...50} # create a hash that pairs keys (letter grade) with values (numerical range). Can you use "..." as a value in a hash? | |
sum = 0 # default the sum to 0 | |
hash.each do |key, val| # iterate and say that in the hash, for each value, you're going to add that value to the sum | |
sum += val # incrimenting the value to the sum | |
end | |
average = sum / val.length # defining average, which is the sum divided by the value length (val.length) | |
# not sure what to do here! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment