Preview:
/*
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){
    cart.push(item);
    quantity.push(1);
  }
}

// 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.
}

// THIS IS FOR YOUR TESTING ONLY.
try {
  addItemToCart("apple");
  addItemToCart("apple");
  addItemToCart("beef");
  addItemToCart("milk");
  console.log(addItemToCart("rice")); // This should print 'item not recognized'
} catch (e) {
  console.log(e);
}
console.log(cart.length); // This should print 3
console.log(quantity[0]); // This should print 2
console.log(getTotal()); // This should print 172
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter