functional programing exercise 30% complated

pull/421/head
Fitsumhelina 10 months ago
parent 0cc15d5141
commit e20812cd5d

@ -0,0 +1,59 @@
const products = [
{ product: 'banana', price: 3 },
{ product: 'mango', price: 6 },
{ product: 'potato', price: 2 },
{ product: 'avocado', price: 8 },
{ product: 'coffee', price: 10 },
{ product: 'tea', price: 20 },
]
// // Print the price of each product using forEach
// const product = (i,item)=> {
// products.forEach(item => {
// console.log(item.price)
// });}
// product()
// Print the product items as follows using forEach
// The price of banana is 3 euros.
// function prod (i,item) {
// products.forEach((item) => {
// console.log(`The price of ${item.product} is ${item.price} euros.`)
// })
// }
// prod()
// Calculate the sum of all the prices using forEach
// const sum = (product) => {
// let sum =0
// products.forEach((product) => sum += product.price)
// console.log(sum)
// }
// sum()
// Create an array of prices using map and store it in a variable prices
// const price =products.map((product) => product.price)
// console.log(price)
// Filter products with prices
const product = products.filter((item) => item.price)
console.log(product)
// Use method chaining to get the sum of the prices(map, filter, reduce)
// Calculate the sum of all the prices using reduce only
// Find the first product which doesn't have a price value
// Find the index of the first product which does not have price value
// Check if some products do not have a price value
// Check if all the products have price value
// Explain the difference between forEach, map, filter and reduce
// Explain the difference between filter, find and findIndex
// Explain the difference between some and every

@ -276,7 +276,7 @@ const products = [
const userRatedProducts = (userId, products) => {
const ratedProducts = products.filter((item) =>
item.ratings.find((rating) => rating.userId === userId)
item.ratings.some((rating) => rating.userId === userId)
);
return ratedProducts.map((item) => item.name);
};

Loading…
Cancel
Save