javascript · 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
Arrays can tell you what is inside them.
Arrays can answer questions. asks: is jam in here? It gives back true or false, so it fits straight into an if.
tells you where jam is, as an index. If it is not there at all, you get -1, because no real place has that number.
For other questions, like which number is the smallest, search by hand. Walk through the array with a loop, and use an if to keep the best one so far.
let fridge = ["milk", "cheese", "jam"];
console.log(fridge.includes("jam"));
console.log(fridge.indexOf("cheese"));
console.log(fridge.indexOf("ham")); Searching is case-sensitive, so "Jam" and "jam" do not match.
guess first
Is there milk in the fridge? Make a guess.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let fridge = ["milk", "cheese", "jam"];
console.log(fridge.includes("Milk"));
your turn
Your turn. Get on the guest list.
Type one of the guest names between the quotes, spelled exactly as it is in the array. Then press run to find out your guest number.
fix it
The coldest day was 4, not 0. Fix the search!
- No temperature here is below 0, so
coldestnever changes. Where does it start? - Start
coldestat a real temperature from the array, like the first one. - Change 0 to temps[0] on line 2, then press run.
nailed it
you can search an array now. :D
.includes()answerstrueorfalse..indexOf()gives the index, or -1 if it is missing.- To find the smallest, start with a real item and compare.
new words in your collection
- includes
- indexOf
This lesson isn’t saved. A free account remembers every lesson you finish.