Skip to content
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
190 changes: 126 additions & 64 deletions starter/src/assets/script.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,141 @@
/* Create an array named products which you will use to add all of your product object literals that you create in the next step. */
// Array to hold the products available for sale
const products = [
{
name: "Cherry",
price: 2,
quantity: 0,
productId: 1,
image: "images/cherry.jpg"
},
{
name: "Orange",
price: 3,
quantity: 0,
productId: 2,
image: "images/orange.jpg"
},
{
name: "Strawberry",
price: 4,
quantity: 0,
productId: 3,
image: "images/strawberry.jpg"
}
];

/* 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)
*/
// Array to hold items in the shopping cart
let cart = [];

/* 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
*/
// Global variable to track the total paid amount
let totalPaid = 0;

/* Declare an empty array named cart to hold the items in the cart */
/**
* Helper function to find a product by its ID.
* @param {number} productId - The ID of the product.
* @returns {object|null} - The product object if found, otherwise null.
*/
function getProductById(productId) {
return products.find(p => p.productId === productId) || null;
}

/* 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
*/
/**
* Adds a product to the cart or increases its quantity if it already exists.
* @param {number} productId - The ID of the product to add.
*/
function addProductToCart(productId) {
let product = getProductById(productId);
if (!product) return;

/* 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
*/
let cartItem = cart.find(item => item.productId === productId);
if (cartItem) {
cartItem.quantity++;
} else {
cart.push({ ...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
*/
/**
* Increases the quantity of a product in the cart.
* @param {number} productId - The ID of the product.
*/
function increaseQuantity(productId) {
let cartItem = cart.find(item => item.productId === productId);
if (cartItem) {
cartItem.quantity++;
}
}

/* 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
*/
/**
* Decreases the quantity of a product in the cart.
* If quantity reaches 0, the product is removed.
* @param {number} productId - The ID of the product.
*/
function decreaseQuantity(productId) {
let cartItem = cart.find(item => item.productId === productId);
if (cartItem) {
cartItem.quantity--;
if (cartItem.quantity === 0) {
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
*/
/**
* Removes a product from the cart.
* @param {number} productId - The ID of the product to remove.
*/
function removeProductFromCart(productId) {
cart = cart.filter(item => item.productId !== productId);
}

/* Create a function called emptyCart that empties the products from the cart */
/**
* Calculates the total cost of all products in the cart.
* @returns {number} - The total price of items in the cart.
*/
function cartTotal() {
return cart.reduce((total, item) => total + item.price * item.quantity, 0);
}

/* 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
*/
/**
* Empties the cart and resets the totalPaid variable.
*/
function emptyCart() {
cart = [];
totalPaid = 0;
}

/* Place stand out suggestions here (stand out suggestions can be found at the bottom of the project rubric.)*/
/**
* Processes a payment and ensures it meets the required conditions.
* If the payment is enough, empty the cart and return the change.
* If the payment is insufficient, return the remaining balance needed.
* @param {number} amount - The amount paid by the customer.
* @returns {number} - The remaining balance or change.
*/
function pay(amount) {
let total = cartTotal(); // Total of items in the cart
let remainingBalance = amount - total; // The difference between the paid amount and total cost

// If the amount paid is enough, empty the cart and return the change
if (amount >= total) {
totalPaid = amount; // Store the paid amount
emptyCart(); // Empty the cart
} else {
totalPaid = amount; // Store the paid amount, but cart won't be emptied if insufficient
}

/* 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
*/
return remainingBalance; // If paid more than required, return the change, else return the remaining balance
}

// Exporting functions for testing
module.exports = {
products,
cart,
addProductToCart,
increaseQuantity,
decreaseQuantity,
removeProductFromCart,
cartTotal,
pay,
emptyCart,
/* Uncomment the following line if completing the currency converter bonus */
// currency
}
products,
cart,
addProductToCart,
increaseQuantity,
decreaseQuantity,
removeProductFromCart,
cartTotal,
pay,
emptyCart,
getProductById
};