diff --git a/Exercises/day-1/functional-programing-exercise.js b/Exercises/day-1/functional-programing-exercise.js new file mode 100644 index 0000000..0679d92 --- /dev/null +++ b/Exercises/day-1/functional-programing-exercise.js @@ -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 \ No newline at end of file diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index abc4785..9973285 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -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); };