Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. var cart = [];
  2.  
  3. function getCart() {
  4. return cart;
  5. }
  6.  
  7. function setCart(c) {
  8. cart = c;
  9. return cart;
  10. }
  11.  
  12. function addToCart(item) {
  13. // write your code here
  14. var price = Math.floor(100*Math.random() + 1);
  15. cart.push({itemName: item, itemPrice: price});
  16. return(`${item} has been added to your cart.`);
  17. }
  18.  
  19. function viewCart() {
  20. // write your code here
  21. if (cart.length === 0){
  22. return ('Your shopping cart is empty.');
  23. } else if (cart.length === 1){
  24. var oneItem = `In your cart, you have ${cart[0].itemName} at $${cart[0].itemPrice}.`;
  25. return (oneItem);
  26. } else if (cart.length === 2){
  27. var twoItems = `In your cart, you have ${cart[0].itemName} at $${cart[0].itemPrice}, and ${cart[1].itemName} at $${cart[1].itemPrice}.`;
  28. return (twoItems);
  29. } else {
  30. var cartData = [];
  31. for (var i = 0; i < cart.length-1; i++){
  32. cartData.push(`${cart[i].itemName} at $${cart[i].itemPrice}`);
  33.  
  34. }
  35. var threePlusItems = `In your cart, you have ${cartData.join(', ')}, and ${cart[cart.length-1].itemName} at $${cart[cart.length-1].itemPrice}.`;
  36.  
  37. return (threePlusItems);
  38. }
  39. }
  40.  
  41. function total() {
  42. // write your code here
  43. var cartItemPrice = [];
  44. var totalPrice = 0;
  45.  
  46. for(var i = 0; i < cart.length; i++) {
  47. var cartPrice = cart[i].itemPrice;
  48. cartItemPrice.push(cartPrice);
  49. }
  50.  
  51. for (var a = 0; a < cartItemPrice.length; a++) {
  52. totalPrice = cartItemPrice[a]+=totalPrice;
  53. }
  54. return totalPrice;
  55.  
  56. }
  57.  
  58. function removeFromCart(item) {
  59. console.log(`start`)
  60. // write your code here
  61. for (var i = 0; i < getCart().length; i++){
  62. console.log(getCart()[i]);
  63. if (getCart()[i].itemName === item){
  64.  
  65. getCart().splice(i, 1);
  66. return cart;
  67.  
  68. }
  69. else {
  70. return (`That item is not in your cart.`);
  71. }
  72. }
  73. console.log(`end`);
  74. }
  75.  
  76. function placeOrder(cardNumber) {
  77. // write your code here
  78. var cartTotal = 0;
  79. cartTotal = total();
  80. if (cardNumber){
  81.  
  82. for (var i = cart.length; i>= 0; i--) {
  83. cart.pop();
  84. return (`Your total cost is $${cartTotal}, which will be charged to the card ${cardNumber}.`);
  85. }
  86.  
  87. }
  88.  
  89. else {
  90. return ("Sorry, we don't have a credit card on file for you.");
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement