From a9eaebdf3e6c2055481da2e2c9bce84b2b3917ad Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 27 Jun 2024 22:44:11 +0800 Subject: [PATCH] Added forEach and reduce solutions --- Week2/Assignment3.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Week2/Assignment3.js b/Week2/Assignment3.js index 1564d94..c0b0ccb 100644 --- a/Week2/Assignment3.js +++ b/Week2/Assignment3.js @@ -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++) { @@ -11,6 +33,7 @@ function calculate(data) { } return totalPrice; } +*/ const discountedPrice = calculate({ discount: 0.1,