-
-
Save iamsok/e3248cb783610674ba06 to your computer and use it in GitHub Desktop.
scores = [75, 100, 85, 65, 84, 87, 95] | |
def avg(array) | |
total = 0 | |
array.each do |grade| | |
total += grade | |
end | |
avg = total / array.length | |
avg | |
end | |
puts avg(scores) | |
def min(array) | |
min = array[0] | |
array.each do |grade| | |
if grade < min | |
min = grade | |
end | |
end | |
min | |
end | |
puts min(scores) | |
def max(array) | |
max = array[-1] | |
array.each do |grade| | |
if grade > max | |
max = grade | |
end | |
end | |
max | |
end | |
puts max(scores) |
Also, in case it's helpful, here's the criteria we use in "grading" these things. The ones towards the bottom will apply to all systems checks:
average method has correct signature
average method calculates average
average method converts to float (OPTIONAL)
average method handles empty list (OPTIONAL)
average method does not modify array
max method has correct signature
max method calculates max
max method does not modify array
max method does not sort the array
max method does not use the Array#max method
min method has correct signature
min method calculates min
min method does not modify array
min method does not sort the array
min method does not use the Array#min method
Proper indentation (2 spaces, no tabs).
Methods and variables have clear, meaningful names.
Method and variable names use underscores, class and module names use camel-case.
No global or instance variables.
Newline included at end of file.
No consecutive (2+) blank lines.
No unnecessary "puts" or "binding.pry" statements
Late for standup without notifying mentor.
Missed standup without notifying mentor.
Absent without notifying mentor.
Excellent work. These look great. Just a few comments:
Average method
avg = total / array.length
is the average. You also don't need to assign a variable in that case, since you aren't referencing it later, so your final method might look something like:Min and max methods
array
, such asnumbers
orgrades
. Then in your each loop you can pass itnumber
,num
, orgrade
in the pipes. It's just a bit more descriptive.Misc.
each
loops and most conditionals. I find it guides the eye and makes code easier to read. Ex.: