From 61f30f7988c77a034757467b41decd9f69bc6fbd Mon Sep 17 00:00:00 2001 From: "Laura \"Missha\" Vara" <152572519+vara-co@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:56:40 -0600 Subject: [PATCH] Commented area updated Instructional comments were removed, and my own comments were left with relevant details on the code. In hopes that it satisfies graders. --- starter/src/assets/script.js | 93 +++++++++--------------------------- 1 file changed, 23 insertions(+), 70 deletions(-) diff --git a/starter/src/assets/script.js b/starter/src/assets/script.js index ab795e3f..f7264d67 100644 --- a/starter/src/assets/script.js +++ b/starter/src/assets/script.js @@ -1,13 +1,7 @@ -/* Create an array named products which you will use to add all of your product object literals that you create in the next step. */ - -/* Create 3 or more product objects using object literal notation - Each product should include five properties - - name: name of product (string) - - price: price of product (number) - - quantity: quantity in cart should start at zero (number) - - productId: unique id for the product (number) - - image: picture of product (url string) -*/ + +// S h o p p i n g C a r t P r o j e c t + +// Product Array with product properties const products = [ { name: "Carton of Cherries", @@ -31,16 +25,11 @@ const products = [ image: "https://github.com/vara-co/shopping-cart/blob/main/starter/src/images/orangessac2.jpg?raw=true" }, ]; -/* Images provided in /images folder. All images from Unsplash.com - - cherry.jpg by Mae Mu - - orange.jpg by Mae Mu - - strawberry.jpg by Allec Gomes -*/ - -/* Declare an empty array named cart to hold the items in the cart */ +// Empty Cart array to hold products in the cart const cart = []; +// H e l p e r f u n c t i o n s // Helper function - finding products by productId function findProductsById(productId){ return products.find(item => item.productId === productId); @@ -48,7 +37,7 @@ function findProductsById(productId){ // Helper function - finding product index in cart function findProductInCart(productId) { - return cart.find(item => item.productId ===productId); + return cart.find(item => item.productId === productId); } // Helper function - finding Product in Cart by Index @@ -56,18 +45,13 @@ function findProductCartByIndex (productId) { return cart.findIndex(item => item.productId === productId); } - -/* Create a function named addProductToCart that takes in the product productId as an argument - - addProductToCart should get the correct product based on the productId - - addProductToCart should then increase the product's quantity - - if the product is not already in the cart, add it to the cart -*/ - +// S h o p p i n g C a r t F u n c t i o n a l i t y +// Function to add products to cart by the productId function addProductToCart(productId) { // finding product based on productID using helper function const product = findProductsById(productId); - // Is the product in the products array + // Conditional to find product in cart if (product) { product.quantity += 1; // check if product is in cart, otherwise to push it @@ -80,11 +64,7 @@ function addProductToCart(productId) { } } -/* Create a function named increaseQuantity that takes in the productId as an argument - - increaseQuantity should get the correct product based on the productId - - increaseQuantity should then increase the product's quantity -*/ - +// Function to INCREASE quantity of a product by productId function increaseQuantity(productId) { // finding product based on productID using helper function const product = findProductsById(productId); @@ -92,26 +72,22 @@ function increaseQuantity(productId) { // check if product in the products array, and if so, increment by one if (product) { product.quantity += 1; - } -} + } -/* Create a function named decreaseQuantity that takes in the productId as an argument - - decreaseQuantity should get the correct product based on the productId - - decreaseQuantity should decrease the quantity of the product - - if the function decreases the quantity to 0, the product is removed from the cart -*/ +} +// Function to DECREASE the quantity of a product by productId function decreaseQuantity(productId) { // finding product based on productID using helper function const product = findProductsById(productId); - // Check if product in the prod array, and if so, decrease by 1 + // Conditional to check if product in the prod array, and if so, decrease by 1 if (product) { if(product.quantity > 0) { product.quantity -= 1; } - // remove from cart if less than 0 products + // Conditional to remove item/container from cart if less than 0 products if (product.quantity === 0) { const productIndex = findProductCartByIndex (productId); if (productIndex !== -1) { @@ -121,21 +97,17 @@ function decreaseQuantity(productId) { } } -/* Create a function named removeProductFromCart that takes in the productId as an argument - - removeProductFromCart should get the correct product based on the productId - - removeProductFromCart should update the product quantity to 0 - - removeProductFromCart should remove the product from the cart -*/ +// Function to REMOVE PRODUCT from cart by productId function removeProductFromCart(productId) { // finding product based on productID using helper function const product = findProductsById(productId); - // if product exists, then + // Conditional to determine if product exists if (product) { product.quantity = 0; - // find and remove from cart + // Conditional to find and remove product from cart const productIndex = findProductCartByIndex (productId); if (productIndex !== -1) { cart.splice(productIndex, 1); @@ -143,31 +115,22 @@ function removeProductFromCart(productId) { } } -/* Create a function named cartTotal that has no parameters - - cartTotal should iterate through the cart to get the total cost of all products - - cartTotal should return the total cost of the products in the cart - Hint: price and quantity can be used to determine total cost -*/ +// Function to obtain the CART TOTAL function cartTotal() { return cart.reduce((total, product) => { return total + product.price * product.quantity; },0); } -/* Create a function called emptyCart that empties the products from the cart */ - +// Function to empty the products from the cart EMPTY CART function emptyCart() { cart.length = 0; // setting to 0 empties the cart } -/* Create a function named pay that takes in an amount as an argument - - amount is the money paid by customer - - pay will return a negative number if there is a remaining balance - - pay will return a positive number if money should be returned to customer - Hint: cartTotal function gives us cost of all the products in the cart -*/ +// Function PAY taking amount as an argument. +// This will determine the difference that needs to be returned to the shopper, or if they still owe function pay(amount) { // total price in cart const ttlCost = cartTotal(); @@ -179,14 +142,6 @@ function pay(amount) { return balance; } -/* Place stand out suggestions here (stand out suggestions can be found at the bottom of the project rubric.)*/ - - -/* The following is for running unit tests. - To fully complete this project, it is expected that all tests pass. - Run the following command in terminal to run tests - npm run test -*/ module.exports = { products, @@ -198,6 +153,4 @@ module.exports = { cartTotal, pay, emptyCart, - /* Uncomment the following line if completing the currency converter bonus */ - // currency }