๐ Scenario: You are managing a small online store's product list. You want to check if certain products exist and find specific products based on their price.
๐ฏ Goal: Build a program that uses the find() method to locate a product with a specific price and the some() method to check if any product is out of stock.
๐ What You'll Learn
Create an array of product objects with exact properties and values
Create a variable for the price to search
Use the find() method to get the product with the given price
Use the some() method to check if any product is out of stock
Print the found product and the result of the stock check
๐ก Why This Matters
๐ Real World
Checking product availability and finding specific items by price is common in online stores and inventory systems.
๐ผ Career
Understanding <code>find()</code> and <code>some()</code> helps in writing efficient code for searching and validating data in JavaScript applications.
Progress0 / 4 steps
1
Create the products array
Create an array called products with these exact objects: { name: 'Laptop', price: 1200, inStock: true }, { name: 'Phone', price: 800, inStock: false }, { name: 'Tablet', price: 600, inStock: true }.
Javascript
Hint
Use square brackets [] to create an array and curly braces {} for each product object.
2
Set the price to find
Create a variable called searchPrice and set it to 800.
Javascript
Hint
Use const to create the variable and assign the number 800.
3
Use find() and some() methods
Use the find() method on products to create a variable foundProduct that finds the product with price equal to searchPrice. Then use the some() method on products to create a variable hasOutOfStock that checks if any product has inStock equal to false.
Javascript
Hint
Use arrow functions inside find() and some() to check the conditions.
4
Print the results
Print foundProduct and hasOutOfStock using two separate console.log() statements.
Javascript
Hint
Use console.log() to show the values in the console.
Practice
(1/5)
1. What does the find method do in JavaScript arrays?
easy
A. Returns the first element that matches a condition or undefined if none
B. Returns true if all elements match a condition
C. Returns the index of the last matching element
D. Returns a new array with elements that match a condition
Solution
Step 1: Understand the purpose of find
The find method searches an array and returns the first element that satisfies the given condition.
Step 2: Compare with other methods
Unlike filter which returns all matching elements, find returns only the first or undefined if none found.
Final Answer:
Returns the first element that matches a condition or undefined if none -> Option A
Quick Check:
find returns first match [OK]
Hint: Remember: find returns one item, some returns true/false [OK]
Common Mistakes:
Confusing find with filter
Thinking find returns a boolean
Assuming find returns all matches
2. Which of the following is the correct syntax to use some on an array arr to check if any element is greater than 10?
easy
A. arr.some(x > 10);
B. arr.some(x => x > 10);
C. arr.some(x => {x > 10});
D. arr.some(x => x > 10) {};
Solution
Step 1: Identify correct arrow function syntax
The some method requires a function that returns true or false. The arrow function x => x > 10 correctly returns a boolean.
Step 2: Check syntax errors in options
arr.some(x > 10); misses the arrow function syntax. arr.some(x => {x > 10}); uses braces but no return statement, so returns undefined. arr.some(x => x > 10) {}; has extra braces after the method call, which is invalid.
Final Answer:
arr.some(x => x > 10); -> Option B
Quick Check:
Arrow function with return expression [OK]
Hint: Use arrow function with expression for some() [OK]
Common Mistakes:
Missing arrow in arrow function
Using braces without return in arrow function
Adding extra braces after method call
3. What is the output of this code?
const numbers = [3, 7, 12, 5];
const result = numbers.find(n => n > 10);
console.log(result);
medium
A. 7
B. undefined
C. 3
D. 12
Solution
Step 1: Understand the find condition
The condition n > 10 looks for the first number greater than 10 in the array.
Step 2: Check array elements in order
The array is [3, 7, 12, 5]. The first element > 10 is 12.
Final Answer:
12 -> Option D
Quick Check:
First number > 10 is 12 [OK]
Hint: Find returns first match, not all matches [OK]
Common Mistakes:
Thinking find returns all matches
Confusing find with some
Choosing first element instead of first matching
4. The following code does not work as expected. What is the problem?