Created
September 8, 2016 15:45
-
-
Save FaisalFehad/d9b43ee6d6682ac410bba5d5779f1ef9 to your computer and use it in GitHub Desktop.
calc_method
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
# A method that takes one array of any size as an argument and prints faild when the average total grades is 50 or more and pass when its less than 50 | |
def calculate(grades) | |
# countes the number of objects in the array and assign it to subjects varible to use it latter for calculating the average | |
subjects = grades.count | |
# sums all of the array objects and assign them to the varible sum | |
sum = grades.inject(:+) | |
# compares sum with the passing grade and prints the resuts | |
if sum / subjects >= 50 | |
puts "Faild" | |
else | |
puts "Passed" | |
end | |
end | |
# tests | |
calculate([20, 43, 5, 49, 32, 38, 22]) # passed | |
calculate([87, 95, 85, 77, 60, 100, 74]) # faild | |
calculate([88, 90]) # faild | |
calculate([20, 40, 49]) # passed | |
calculate([50]) # faild | |
# output | |
[faisal@X260 ruby]$ ruby calc_method.rb | |
Passed | |
Faild | |
Faild | |
Passed | |
Faild | |
[faisal@X260 ruby]$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment