Simple Shoppingcart Function Uisng Javascript


Thu Jul 08 2021 08:20:50 GMT+0000 (UTC)

Saved by @chisomloius #undefined ##array ##2d-array

You are developing an ecommerce application which allows users to add items to a virtual shopping cart. Once the user is done shopping, we will need to know the total amount of money to charge the user based on what was purchased.

// The cart array stores all of the items selected in the shopping cart

let cart = [];

// The quantity array stores the quantity of the items in the cart.
// The cart and quantity arrays are connected based on the index i.e
// given cart = ["bread", "apple"] and quantity = [2, 1]
// we can tell that we have two pieces of bread and 1 apple.
// The item at index 0 in cart i.e cart[0] is "bread", so we get the quantity
// at the same index in the quantity array i.e quantity[0]

let quantity = [];

// The prices multidimensional array stores the prices of the items in the
// store. There are 6 items that can be purchased from the store:
// "bread", "apple", "noodles", "beef", "milk" and "coke".
// The price for "bread" is 20, the price for "apple" is 50 and so on.

const prices = [
  ["bread", 20],
  ["apple", 50],
  ["noodles", 100],
  ["beef", 40],
  ["milk", 32],
  ["coke", 25],

// Given an item, Complete the code to add the item to the cart and quantity
// arrays.
// Don't touch this section, we will cover functions later.
function addItemToCart(item) {
  // item is a variable that represents the item that is being added to the
  // cart. Valid items are items present in the prices array i.e "bread",
  // "apple", "noodles", "beef", "milk" and "coke".
  // An invalid item e.g "rice"  should throw an error ‘item not recognized’
  let isValidItem = false;
  // TODO(1): Write code to set isValidItem to true if item is present in the prices array
  for(let i = 0; i < prices.length; i++){
        if(item === prices[i][0]){
            isValidItem = true;
  // TODO(2): Throw error "item not recognized" if isValidItem !== true i.e false
  if(isValidItem !== true){
        throw "item not recognized"
  let foundItem = false;
  // TODO(3): Write code to check if item is in the cart array, and if so set
  // foundItem = true, and increase the quantity of the item by 1
  // Hint: Use the index where the item was found in the cart to increase the
  // quanity in the quantity array
  for(let i = 0; i < cart.length; i++){
    if(item === cart[i]){
        foundItem = true;
        quantity[i] += 1 
  // TODO(4): Write code to add item to cart and set quantity to 1, if item is
  // was not found in the cart i.e foundItem is not true.
  // Hint: To set the quantity for the recently added item to 1,
  // do quantity.push(1)
  if(foundItem !== true){

// Write the code to calculate the total cost of all items in the cart based on
// the items in the cart and the quantity of the items purchased. The total
// cost should be stored in the total variable
// Don't touch the next line, we will cover functions later.
function getTotal() {
  let total = 0;

  for (let i = 0; i < cart.length; i++) {
    let item = cart[i];
    let qty = quantity[i];
    let subtotal = 0;
    // TODO(7) : Loop through the prices array to get the price of item, then
    // compute and set subtotal by multiplying the price and qty.
    // Hint: Each array in prices has the item at index 0, and the price at
    // index 1
  for(let itemPrice = 0; itemPrice < prices.length; itemPrice++){
        let priceList = prices[itemPrice]
        let price = priceList[1];
        let priceItem = priceList[0]        
        if(item === priceItem){
            subtotal = price * qty
    total += subtotal;
    // TODO(8): Add subtotal to total (this should after the loop above)
  return total; // Do not edit or remove this line.

try {
  console.log(addItemToCart("rice")); // This should print 'item not recognized'
} catch (e) {
console.log(cart.length); // This should print 3
console.log(quantity[0]); // This should print 2
console.log(getTotal()); // This should print 172