【问题标题】:Adding the results of multiple alerts that is inside a for loop添加 for 循环内的多个警报的结果
【发布时间】:2016-07-20 02:56:06
【问题描述】:

我使用此代码的目标是创建一个临时购物车,让您可以选择从一小部分商品中进行选择、选择数量、获取总数、删除商品(如果需要)并继续“结帐”。我的问题是,一旦我通过项目类型(通过警报)获得价格结果,我不知道如何将所有潜在警报添加在一起,因为它在 for 循环中运行。有谁知道如何做到这一点?我对JS相当陌生,不知道如何进行。这也是我在堆栈上的第一个问题,如果这是一个奇怪的问题,请原谅我。

这是我的代码:

var item = function(itemName, itemPrice, itemTax) {
  this.products = itemName;
  this.cost = itemPrice;
  this.taxes = itemTax;
  this.total = itemPrice + (itemPrice * itemTax)
}

var list = [];

list[0] = new item("Shoes", 67.99, .075);
list[1] = new item("Coat", 78.99, .075);
list[2] = new item("Book", 9.99, .075);
list[3] = new item("Suitcase", 56.99, .075);

function theStore() {
  var greeting = prompt("Welcome to xxxxxxxx! Do you want to begin shopping?");



  while (greeting === "Yes")
  {

      for (var i = 0; i < list.length; i++) 
      {
        var j = list[i];
        var adding = prompt("The " + j.products + " cost " + j.cost + ". Would you like to add it to your cart?")
        if (adding === "Yes")
        {
          var addingMore = prompt("How much do you want?")
          if (addingMore < 1)
          {
            alert("This item was not added to your cart.") 
          }

          else
          {
            alert(addingMore + " of " + j.products +" has been added to your cart." + " Your total for this kind of item is " + addingMore * (j.cost) + ".")
          }

      }
      else
      {
        alert(j.products + " was not added to your cart.")
      }
    }

    greeting = prompt("Do you want to continue shopping? Yes or No");


  }
}

theStore()

理想情况下,当用户提示“否”(第 47 行)时,我想向他们显示所有项目的总数。

感谢任何帮助/提示/建议。

【问题讨论】:

    标签: javascript for-loop while-loop alert


    【解决方案1】:

    在循环外声明一个变量totalAmount,并在用户将东西添加到购物车时添加到它。然后当用户没有点击警报时,总金额。

    我添加了一个小计变量,该变量由用户选择项目数量时计算得出。小计是添加到最后提醒的变量 totalAmount 中。

    var item = function(itemName, itemPrice, itemTax) {
      this.products = itemName;
      this.cost = itemPrice;
      this.taxes = itemTax;
      this.total = itemPrice + (itemPrice * itemTax)
    }
    
    var list = [];
    var totalAmount = 0;
    
    list[0] = new item("Shoes", 67.99, .075);
    list[1] = new item("Coat", 78.99, .075);
    list[2] = new item("Book", 9.99, .075);
    list[3] = new item("Suitcase", 56.99, .075);
    
    function theStore() {
      var greeting = prompt("Welcome to xxxxxxxx! Do you want to begin shopping?");
    
    
    
      while (greeting === "Yes")
      {
    
          for (var i = 0; i < list.length; i++) 
          {
            var j = list[i];
            var adding = prompt("The " + j.products + " cost " + j.cost + ". Would you like to add it to your cart?")
            if (adding === "Yes")
            {
              var addingMore = prompt("How much do you want?")
              if (addingMore < 1)
              {
                alert("This item was not added to your cart.") 
              }
    
              else
              {
                var subTotal = addingMore * (j.cost);
                totalAmount += subTotal;
                alert(addingMore + " of " + j.products +" has been added to your cart." + " Your total for this kind of item is " + subTotal.toString() + ".")
              }
    
          }
          else
          {
            alert(j.products + " was not added to your cart.")
          }
        }
    
        greeting = prompt("Do you want to continue shopping? Yes or No");
    
        if(greeting === "No"){
            alert("Your Total Amount is: " + totalAmount);
        }
    
      }
    }
    
    theStore()
    

    【讨论】:

      【解决方案2】:
        var item = function(itemName, itemPrice, itemTax) {
        this.products = itemName;
        this.cost = itemPrice;
        this.taxes = itemTax;
        this.total = itemPrice + (itemPrice * itemTax)
      }
      
      var list = [];
      
      list[0] = new item("Shoes", 67.99, .075);
      list[1] = new item("Coat", 78.99, .075);
      list[2] = new item("Book", 9.99, .075);
      list[3] = new item("Suitcase", 56.99, .075);
      
          var selected_items = [];
      
          function theStore() {
            var greeting = confirm("Welcome to xxxxxxxx! Do you want to begin shopping?");
      
      
      
            while (greeting)
            {
      
                for (var i = 0; i < list.length; i++) 
                {
                  var j = list[i];
                  var adding = confirm("The " + j.products + " cost " + j.cost + ". Would you like to add it to your cart?")
                  if (adding)
                  {
                    var addingMore = prompt("How much do you want?")
                    if (addingMore < 1)
                    {
                      alert("This item was not added to your cart.") 
                    }
      
                    else
                    {
                      alert(addingMore + " of " + j.products +" has been added to your cart." + " Your total for this kind of item is " + addingMore * (j.cost) + ".");
          j.quantity = addingMore;              // Add user selected quantity to object
      
          j.total_cost = addingMore * (j.cost); // Add cost if user selected quantity to object
      
          selected_items.push(j);               // Let's store it in array.
                    }
      
                }
                else
                {
                  alert(j.products + " was not added to your cart.")
                }
              }
      
              greeting = confirm("Do you want to continue shopping? Yes or No");
      
            return selected_items;
            }
          }
      
          // theStore() Stored array can be utilised for processing.
          console.log(theStore());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-01
        • 2019-09-19
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        相关资源
        最近更新 更多