Skip to content

Commit

Permalink
Commented area updated
Browse files Browse the repository at this point in the history
Instructional comments were removed, and my own comments were left with relevant details on the code. In hopes that it satisfies graders.
  • Loading branch information
vara-co authored Oct 29, 2024
1 parent 4268f4e commit 61f30f7
Showing 1 changed file with 23 additions and 70 deletions.
93 changes: 23 additions & 70 deletions starter/src/assets/script.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -31,43 +25,33 @@ 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);
}

// 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
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
Expand All @@ -80,38 +64,30 @@ 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);

// 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) {
Expand All @@ -121,53 +97,40 @@ 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);
}
}
}

/* 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();
Expand All @@ -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,
Expand All @@ -198,6 +153,4 @@ module.exports = {
cartTotal,
pay,
emptyCart,
/* Uncomment the following line if completing the currency converter bonus */
// currency
}

0 comments on commit 61f30f7

Please sign in to comment.