typescript · 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 whether it holds something.
asks an array a yes-or-no question: is this item in you? temps.includes(19) gives true if 19 is there, and false if not.
goes further and tells you where it is. It gives the index of the first match, or -1 when there is no match, because no real index is below 0.
gives the bigger number, 9, and Math.min gives the smaller. Use one in a loop to find the warmest day: keep the best so far, and let each item challenge it.
let temps: number[] = [14, 19, 11, 17];
console.log(temps.includes(19));
console.log(temps.indexOf(11));
console.log(temps.indexOf(30));
console.log(Math.max(4, 9), Math.min(4, 9)); .includes() and .indexOf() look for an exact match, so "Ava" and "ava" are different.
guess first
Hunting for the warmest day. What will it show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let temps: number[] = [14, 19, 11, 17];
let warmest: number = temps[0];
for (const temp of temps) {
warmest = Math.max(warmest, temp);
}
console.log(warmest);
your turn
Your turn. Get on the guest list.
Push your name onto the guest list in the first blank. Then check for the same name in the second blank, spelled exactly the same, and press run.
fix it
The coldest day was 0? Fix the search!
- It says 0, but 0 is not even in the array. Where does 0 come from?
- coldest starts at 0, and no temperature here is colder than that, so it never changes.
- Start with the first item instead: let coldest: number = temps[0]; then press run.
nailed it
you can search any array now. :D
.includes()says whether an item is in the array..indexOf()gives its index, or-1if it is missing.- Start the best so far at the first item, not 0.
new words in your collection
- includes
- indexOf
- Math.max
This lesson isn’t saved. A free account remembers every lesson you finish.