Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my first commit #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions starter/src/assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,131 @@
- productId: unique id for the product (number)
- image: picture of product (url string)
*/

let products = [
{
name:'Cherry',
price: 4,
quantity: 0,
productId: 1,
image:'./images/cherry.jpg'
},
{
name:'Oranges',
price: 10,
quantity: 0,
productId: 2,
image:'./images/orange.jpg'
},
{
name:'Strawberry',
price: 20,
quantity: 0,
productId: 3,
image:'./images/strawberry.jpg'
}
]
/* 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 */
let cart = [

]
/* 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
*/
function addProductToCart (productId) {
const product = products.find((p)=> p.productId === productId);
product.quantity++
const productInCart = cart.find((item)=> item.productId === productId);

if(!productInCart){
cart.push(product)
}
}


/* 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 increaseQuantity (productId){
const product = cart.find((item)=> item.productId ===productId);

if(product){
product.quantity++;
}
}
/* 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 decreaseQuantity(productId) {
const product = cart.find((item)=> item.productId === productId);

if (product){
product.quantity--;
if (product.quantity === 0){
//Remove product from cart if quantity 0
let productIndex = cart.findIndex((a)=>cart.productId == productId)
if(productIndex)
cart.splice(productIndex, 1)
}
}
}

/* 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 removeProductFromCart(productId) {
let productIndexInCart;
let productToRemove = cart.find((p, i)=>{
productIndexInCart = i;
return p.productId == productId
})

if(productToRemove){
productToRemove.quantity = 0
cart.splice(productIndexInCart, 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 cartTotal () {
let total = 0;
cart.map((item)=>{
total += item.quantity * item.price
}, 0);
return total
}

/* Create a function called emptyCart that empties the products from the cart */

function emptyCart() {
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(amount) {
const total = cartTotal();
const remainingBalance = amount - total;
return remainingBalance;
}

/* Place stand out suggestions here (stand out suggestions can be found at the bottom of the project rubric.)*/

Expand Down
Loading