查看购物车功能未返回正确的值

时间:2018-02-24 07:23:20

标签: javascript arrays oop object

所以我的目标是创建一个带有vanilla JS的伪购物车,用于编码分配,其中一个目标是创建一个不接受任何参数的viewCart()函数。

说明:它应该遍布购物车中的每件商品,将这些内容打印成以下格式的长篇连贯的声明:在购物车中,您的香蕉价格为17美元,煎饼面糊价格为5美元,鸡蛋价格为49美元。 / p>

条件:

  • 如果购物车为空,则该功能应该打印出来:您的 购物车是空的。
  • 1件样品输出:在您的购物车中,您的香蕉价格为17美元。
  • 2项 - 在你的购物车中,你有17美元的香蕉和煎饼面糊 $ 5
  • 3件以上 - 在您的购物车中,您有17美元的香蕉,煎饼面糊 5美元,鸡蛋49美元。

到目前为止,我已成功完成前两个条件并几乎完成了第三个条件。但是,我的函数将返回如下内容:

在你的购物车中,香蕉价格为17美元,煎饼面糊为5美元,价格为49美元。

而不是:

在购物车中,您的香蕉价格为17美元,煎饼面糊价格为5美元,鸡蛋价格为49美元。

我在函数中添加了一些注释,以帮助您调试(如果需要)。

  var cart = [];

    function getCart() {
     return cart;
    }

    function setCart(c) {
      cart = c;
      return cart;
    }


    function addToCart(itemName) {
     var object = {[itemName]: Math.floor(Math.random(1,100) * 100)};
     cart.push(object);
     console.log(`${itemName} has been added to your cart`);
     return cart;
    }



 function viewCarted() {
  if(!cart.length) {
    console.log('Your shopping cart is empty.');
  }
  else {
    var items = [];
    // for loop to iterate over objects in cart
    for(var i=0; i < cart.length; i++) {
      // another for loop to iterate over the object's keys
      for(var item in cart[i]) {
        // adding the key and value into the items array
        items.push(item + " at $" + cart[i][item]);
      }
    }
    //  checking if items array length is equal to 2
    // if it is, then it'll add an "and" after the first comma and finish the function
    // if not then output will be: In your cart, you have banana at $77.
    if(items.length == 2) {
      return(`In your cart, you have ${items.join(" and ") + "."}`);
    } 
    // otherwise if there is three or more items in the cart then these conditions will activate
    else {
      // another for loop to iterate over the cart array
      for(var x=0; x < cart.length; x++) {
        // testing to see the indexes: 0,1,2
        console.log(x)
        // condition to see if it isn't the last item in cart
        if(x+1 != cart.length) {
           console.log(`In your cart, you have ${items.join(", ") + "."}`);
        } 
        // condition if it is the last item
        else {
          const mostItems = items.slice(0, -1);
          const lastItem = items.slice(-1)[ 0 ];
          console.log(`In your cart, you have ${mostItems.join(", and ")}${lastItem}.`);
        }
      }
    }
  }
}

  addToCart("banana")
  addToCart("a")
  addToCart("b")
  viewCarted();

输出:

0
In your cart, you have banana at $40, a at $62, b at $75.
1
In your cart, you have banana at $40, a at $62, b at $75.
2
In your cart, you have banana at $40, and a at $62b at $75.

1 个答案:

答案 0 :(得分:0)

您正在使用, and ...加入所有项目,而不是最后一个条件中的最后一项。

相反,您可以将它们分成“全-1”和“最后”,如下所示:

    else {
       const mostItems = items.slice(0, -1);
       const lastItem = items.slice(-1)[ 0 ];
       console.log(`In your cart, you have ${mostItems.join(", and ")}${lastItem}.`);
    }

将其放在以下else

    // condition to see if it isn't the last item in cart
    if(x+1 != cart.length) {
       console.log(`In your cart, you have ${items.join(", ") + "."}`);
    } 
    // condition if it is the last item
    else {
       // new stuff goes here:


       // OLD STUFF
       // console.log(`In your cart, you have ${items.join(", and ") + "."}`);
    }