Last active
December 20, 2015 07:19
-
-
Save MrBean83/6092939 to your computer and use it in GitHub Desktop.
"Count the numbers in an array between a given range"
This file contains hidden or 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 count_between(array, lower_bound, upper_bound) | |
| count = 0 | |
| array.each do |num| | |
| if ((num >= lower_bound) && (num <= upper_bound)) | |
| count += 1 | |
| end | |
| end | |
| return count | |
| end |
I went a different route though, I went with
array.select { |x| x <= upper_bound && x >= lower_bound }.size
ohhhh, you don't need the array.compact and array.count
all you need to put is count
def count_between(array, lower_bound, upper_bound)
count = 0
array.each do |num|
if ((num >= lower_bound) && (num <= higher_bound))
count += 1
eliminate the else part of the statement, it's understood that if it doesn't fulfill the if statement it doesn't do anything
end
return count
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh wow, you're really close. Instead of trying to get a count on the array, start a counter and after you array.each do I add += 1 with a conditional statement that it's within the range.