Sfida: Ricerca di Elementi Utilizzando find()
Compito
- L'array originale è fornito come
products, contenente oggetti che rappresentano prodotti con le proprietàname,priceefeatured. - Utilizzare il metodo
find()per individuare il primo prodotto nell'array con la proprietàfeaturedimpostata sutrue. - Se viene trovato un prodotto in evidenza, registrare il suo nome e prezzo; altrimenti, registrare un messaggio che indica che non è stato trovato alcun prodotto in evidenza.
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(___); }
Output previsto:
Featured product: Headphones, Price: $150
- Utilizzare il metodo
find()sull'arrayproductse fornire una funzione di callback che verifichi se la proprietàfeatureddell'oggetto prodotto ètrue. - Se viene trovato un prodotto in evidenza, registrare il suo nome e prezzo; altrimenti, registrare un messaggio che indichi che non è stato trovato alcun prodotto in evidenza.
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."); }
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 5. Capitolo 6
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Suggested prompts:
Can you explain how the find() method works in this context?
What happens if there are no featured products in the array?
Can you show how to modify the code to find all featured products instead of just the first one?
Awesome!
Completion rate improved to 2.27
Sfida: Ricerca di Elementi Utilizzando find()
Scorri per mostrare il menu
Compito
- L'array originale è fornito come
products, contenente oggetti che rappresentano prodotti con le proprietàname,priceefeatured. - Utilizzare il metodo
find()per individuare il primo prodotto nell'array con la proprietàfeaturedimpostata sutrue. - Se viene trovato un prodotto in evidenza, registrare il suo nome e prezzo; altrimenti, registrare un messaggio che indica che non è stato trovato alcun prodotto in evidenza.
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(___); }
Output previsto:
Featured product: Headphones, Price: $150
- Utilizzare il metodo
find()sull'arrayproductse fornire una funzione di callback che verifichi se la proprietàfeatureddell'oggetto prodotto ètrue. - Se viene trovato un prodotto in evidenza, registrare il suo nome e prezzo; altrimenti, registrare un messaggio che indichi che non è stato trovato alcun prodotto in evidenza.
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."); }
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 5. Capitolo 6