Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var cart = [];
- function getCart() {
- return cart;
- }
- function setCart(c) {
- cart = c;
- return cart;
- }
- function addToCart(item) {
- // write your code here
- var price = Math.floor(100*Math.random() + 1);
- cart.push({itemName: item, itemPrice: price});
- return(`${item} has been added to your cart.`);
- }
- function viewCart() {
- // write your code here
- if (cart.length === 0){
- return ('Your shopping cart is empty.');
- } else if (cart.length === 1){
- var oneItem = `In your cart, you have ${cart[0].itemName} at $${cart[0].itemPrice}.`;
- return (oneItem);
- } else if (cart.length === 2){
- var twoItems = `In your cart, you have ${cart[0].itemName} at $${cart[0].itemPrice}, and ${cart[1].itemName} at $${cart[1].itemPrice}.`;
- return (twoItems);
- } else {
- var cartData = [];
- for (var i = 0; i < cart.length-1; i++){
- cartData.push(`${cart[i].itemName} at $${cart[i].itemPrice}`);
- }
- var threePlusItems = `In your cart, you have ${cartData.join(', ')}, and ${cart[cart.length-1].itemName} at $${cart[cart.length-1].itemPrice}.`;
- return (threePlusItems);
- }
- }
- function total() {
- // write your code here
- var cartItemPrice = [];
- var totalPrice = 0;
- for(var i = 0; i < cart.length; i++) {
- var cartPrice = cart[i].itemPrice;
- cartItemPrice.push(cartPrice);
- }
- for (var a = 0; a < cartItemPrice.length; a++) {
- totalPrice = cartItemPrice[a]+=totalPrice;
- }
- return totalPrice;
- }
- function removeFromCart(item) {
- console.log(`start`)
- // write your code here
- for (var i = 0; i < getCart().length; i++){
- console.log(getCart()[i]);
- if (getCart()[i].itemName === item){
- getCart().splice(i, 1);
- return cart;
- }
- else {
- return (`That item is not in your cart.`);
- }
- }
- console.log(`end`);
- }
- function placeOrder(cardNumber) {
- // write your code here
- var cartTotal = 0;
- cartTotal = total();
- if (cardNumber){
- for (var i = cart.length; i>= 0; i--) {
- cart.pop();
- return (`Your total cost is $${cartTotal}, which will be charged to the card ${cardNumber}.`);
- }
- }
- else {
- return ("Sorry, we don't have a credit card on file for you.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement