🤔
💨
Our robot helper checks each car against a condition!
The find() Method
The find() method searches for the first item that passes a test.
You give it a function (the test), and it returns the first item that makes the function return true.
💡
💨
How find() Works
1const numbers = [5, 12, 8, 130, 44];23// Find first number greater than 104const big = numbers.find(num => num > 10);5console.log(big); // 1267// Find first even number8const even = numbers.find(num => num % 2 === 0);9console.log(even); // 121011// If nothing matches, returns undefined12const huge = numbers.find(num => num > 200);13console.log(huge); // undefined
🤔
💨
The Arrow Function
The test we give to find() is called a "callback function". Here's what it means:
1// This arrow function:2num => num > 1034// Is the same as:5function(num) {6 return num > 10;7}89// It checks: "Is this number greater than 10?"
🎮
Try It Yourself!
Click a condition to find the first number that matches! Watch the robot scan each car.
[
5
[0]12
[1]8
[2]130
[3]44
[4]]
length: 5