Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Search for Items Using find() | Advanced Array Methods and Transformations
JavaScript Data Structures

bookChallenge: Search for Items Using find()

Task

  1. The original array is given as products, containing objects representing products with properties name, price, and featured.
  2. Use the find() method to discover the first product in the array with the featured property set to true.
  3. If a featured product is found, log its name and price; otherwise, log a message indicating that no featured product was found.
12345678910111213141516
const 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(___); }
copy

Expected output:

Featured product: Headphones, Price: $150
  1. Use the find() method on the products array and provide a callback function that checks if the featured property of the product object is true.
  2. If a featured product is found, log its name and price; otherwise, log a message indicating that no featured product was found.
12345678910111213141516
const 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."); }
copy

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 6

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 2.27

bookChallenge: Search for Items Using find()

Svep för att visa menyn

Task

  1. The original array is given as products, containing objects representing products with properties name, price, and featured.
  2. Use the find() method to discover the first product in the array with the featured property set to true.
  3. If a featured product is found, log its name and price; otherwise, log a message indicating that no featured product was found.
12345678910111213141516
const 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(___); }
copy

Expected output:

Featured product: Headphones, Price: $150
  1. Use the find() method on the products array and provide a callback function that checks if the featured property of the product object is true.
  2. If a featured product is found, log its name and price; otherwise, log a message indicating that no featured product was found.
12345678910111213141516
const 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."); }
copy

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 6
some-alt