【问题标题】:How to create an array from user input from dialog boxes prompt()如何从对话框提示()中的用户输入创建数组
【发布时间】:2019-01-20 14:10:34
【问题描述】:

我正在努力选择和编写正确的代码,以便将用户输入从对话框提示符循环到数组中。

我已经被困了几天了。我是编码初学者,尤其是 javascript。

alert("Welcome to my store!");
do{
    var nameArray;
    var name = prompt("Enter Product Name");
    var price = parseInt(prompt("Unit Price of Product in dollars: "));       
    var quantity = parseInt(prompt("Quanity of the product: "));
    var sum =price*quantity;
    userSelection = confirm("Do you want to purchase more?");
}
while(userSelection==true);
for(i=0;i>nameArray.length;i++){
    name[i]++;
    var totalSum = sum[i]++;
}
alert("You bought " +nameArray[]+ ". Your Total cost today is $"+totalSum+".");

我希望用户输入产品名称、价格和数量。我想创建一个警报,它将提取名称并显示它们,并计算总和。

【问题讨论】:

    标签: javascript arrays dialog


    【解决方案1】:

    我有一个小提琴并让它工作,主要是添加一个篮子数组来跟踪用户添加的项目。

    alert("Welcome to my store!");
    var basket = [];
    var totalSum = 0;
    var items = "";
    do{
        var name = prompt("Enter Product Name");
        var price = parseInt(prompt("Unit Price of Product in dollars: "));       
        var quantity = parseInt(prompt("Quanity of the product: "));
        var sum =price*quantity;
        basket.push( {name: name, price: price, quantity: quantity, sum: sum});
        userSelection = confirm("Do you want to purchase more?");
    }
    while(userSelection==true);
    for(i=0;i<basket.length;i++){
        totalSum += basket[i].sum;
        items += basket[i].name+", ";
    }
    alert("You bought " +items+ ". Your Total cost today is $"+totalSum+".");
    

    【讨论】:

      【解决方案2】:

      尝试以下解决方案

      alert('Welcome to my store!');
      
      const items = [];
      
      do {
          const name = prompt('Enter product name');
          const price = parseInt(prompt('Unit Price of Product in dollars'));
          const quantity = parseInt(prompt('Quanity of the product'));
          const sum = price * quantity;
      
          const item = {
              name: name,
              price: price,
              quantity: quantity,
              total: sum
          };
      
          items.push(item);
      } while (confirm('Do you want to purchase more?'));
      
      const totalCost = items.reduce((accumulator, currentValue) => accumulator += currentValue.total, 0);
      
      alert(`You bought ${JSON.stringify(items, null, 4)} Your Total cost today is ${totalCost}`);
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <meta http-equiv="X-UA-Compatible" content="ie=edge">
          <title>Document</title>
      </head>
      <body>
      </body>
      </html>

      您可以阅读有关 reduce here 和 JSON.stringify here 的信息

      多鼓励,这是一个伟大而美丽的世界

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-13
        • 2014-11-06
        • 1970-01-01
        • 1970-01-01
        • 2011-02-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多