Skip to content

Commit

Permalink
Added forEach and reduce solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
yinxoxo committed Jun 27, 2024
1 parent a8950b5 commit a9eaebd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Week2/Assignment3.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
// for Each
function calculate(data) {
// your code here
const { discount, products } = data;
let totalPrice = 0;

products.forEach((product) => {
totalPrice += product.price * (1 - discount);
});

return totalPrice;
}

// .reduce
/*function calculate(data) {
const { discount, products } = data;
return products.reduce(
(total, product) => total + product.price * (1 - discount),
0
);
}
*/

//for loop
/*function calculate(data) {
const { discount, products } = data;
let totalPrice = 0;
for (let i = 0; i < products.length; i++) {
Expand All @@ -11,6 +33,7 @@ function calculate(data) {
}
return totalPrice;
}
*/

const discountedPrice = calculate({
discount: 0.1,
Expand Down

0 comments on commit a9eaebd

Please sign in to comment.