Searching
JavaScript · Unit 8: More on arrays
- Full plan
- JavaScript
- 11 exercises
find searches an array for the first item that meets a condition and hands it to you. The condition is an arrow function, like in filter.
If none of them meets it, you get undefined.
const nums = [4, 9, 12, 7];
const big = nums.find((n) => n > 8);
console.log(big);Prints
9
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Predict the output
What does this code print?
3. Complete the code
Complete it to print Liam's grade.
4. Predict the output
What does this code print?
5. Multiple choice
You have an array of items, each with a
nameand aprice. Which method hands you the bread object directly, so you can read its price?6. Predict the output
What does this code print?
7. Complete the code
Complete it so it says "All done" only if every chore is done.
8. Find the bug
This program should warn you if there are minors in the group. There is one here, aged 15. Which line has the error?
9. Find the case that fails
priceOf(items, name)should return the price of the item with that name, or 0 if it isn't there. Which call does it fail with?10. Put the lines in order
Order the function that returns the name of the first student who passed (6 or more). There's always at least one.
11. Predict the output
You check what you still need to buy for a recipe. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.