ruby · level 1 · unit 6 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Ask an array questions with , , and .
Arrays know some handy methods. .include? asks whether an item is in the array. Methods ending in ?, like .even?, always answer with true or false.
For arrays of numbers, .sum adds up every item, .max finds the biggest and .min finds the smallest. No loop needed.
Need something trickier, like every score over 5? Walk through with .each and check each item with an if. Add break when you only want the first match.
scores = [4, 9, 2, 7]
puts scores.include?(9)
puts scores.sum
puts scores.max
puts scores.min include? is case-sensitive, so "Dog" and "dog" are different items.
guess first
Is the cat on the list? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
pets = ["Cat", "Dog", "Fish"]
puts pets.include?("Dog")
puts pets.include?("cat")
your turn
Your turn. Find the high scores.
Type four scores from 0 to 100. Make sure at least one is over 50, then press run to find the high scores and the best of all.
fix it
The shopping bill will not add up. Fix it!
- Read the error. It is a TypeError. What kind of items are in the array?
- The prices have quotes, so they are strings.
.sumcan only add numbers. - Remove the quotes so the array is [2, 5, 3], then press run.
nailed it
you can search any array now. :D
.include?asks whether an item is in the array..sum,.maxand.minwork on numbers, no loop needed.- Use
.eachwith aniffor trickier searches.
new words in your collection
- include?
- sum
- max
- min
This lesson isn’t saved. A free account remembers every lesson you finish.