Challenge: Search for Items Using find()
Task
- The original array is given as
products
, containing objects representing products with propertiesname
,price
, andfeatured
. - Use the
find()
method to discover the first product in the array with thefeatured
property set totrue
. - If a featured product is found, log its name and price; otherwise, log a message indicating that no featured product was found.
12345678910111213141516const products = [ { name: "Laptop", price: 1200, featured: false }, { name: "Headphones", price: 150, featured: true }, { name: "Smartphone", price: 800, featured: false }, { name: "Camera", price: 1000, featured: true }, ]; const featuredProduct = ___.___((product) => ___ === true); if (featuredProduct) { console.log( `Featured product: ${___}, Price: $${featuredProduct.___}` ); } else { console.log(___); }
Expected output:
Featured product: Headphones, Price: $150
- Use the
find()
method on theproducts
array and provide a callback function that checks if thefeatured
property of the product object istrue
. - If a featured product is found, log its name and price; otherwise, log a message indicating that no featured product was found.
12345678910111213141516const products = [ { name: "Laptop", price: 1200, featured: false }, { name: "Headphones", price: 150, featured: true }, { name: "Smartphone", price: 800, featured: false }, { name: "Camera", price: 1000, featured: true }, ]; const featuredProduct = products.find((product) => product.featured === true); if (featuredProduct) { console.log( `Featured product: ${featuredProduct.name}, Price: $${featuredProduct.price}` ); } else { console.log("No featured product found."); }
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 5. Розділ 6
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 2.27
Challenge: Search for Items Using find()
Свайпніть щоб показати меню
Task
- The original array is given as
products
, containing objects representing products with propertiesname
,price
, andfeatured
. - Use the
find()
method to discover the first product in the array with thefeatured
property set totrue
. - If a featured product is found, log its name and price; otherwise, log a message indicating that no featured product was found.
12345678910111213141516const products = [ { name: "Laptop", price: 1200, featured: false }, { name: "Headphones", price: 150, featured: true }, { name: "Smartphone", price: 800, featured: false }, { name: "Camera", price: 1000, featured: true }, ]; const featuredProduct = ___.___((product) => ___ === true); if (featuredProduct) { console.log( `Featured product: ${___}, Price: $${featuredProduct.___}` ); } else { console.log(___); }
Expected output:
Featured product: Headphones, Price: $150
- Use the
find()
method on theproducts
array and provide a callback function that checks if thefeatured
property of the product object istrue
. - If a featured product is found, log its name and price; otherwise, log a message indicating that no featured product was found.
12345678910111213141516const products = [ { name: "Laptop", price: 1200, featured: false }, { name: "Headphones", price: 150, featured: true }, { name: "Smartphone", price: 800, featured: false }, { name: "Camera", price: 1000, featured: true }, ]; const featuredProduct = products.find((product) => product.featured === true); if (featuredProduct) { console.log( `Featured product: ${featuredProduct.name}, Price: $${featuredProduct.price}` ); } else { console.log("No featured product found."); }
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 5. Розділ 6