javascript中输入的总和

Total sum of an input in javascript

本文关键字:输入 javascript      更新时间:2023-09-26

我试图找到用户在弹出窗口中输入的价格的总和,并显示总数。我需要在输出下面再加一行吗?我正在尝试沿outMsg添加总数,但没有成功。感谢

  <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Array 02</title>
</head>

  <form action="">
<p>BOOK SALE</p>
<p id="Books"</p>
<input type="button" value="Start" onClick="Books()";>
</form>  
<body>
<script>
    function Books()
    {
            //define variables 
            var  arraySize = 2;
            var bookName = new Array(arraySize);
            var bookPrice = new Array(arraySize);
            var total = 0;
            var outMsg      = "";
            for(var i = 0; i < arraySize; i++)
            {
                    bookName[i] = prompt('Enter Book Name:  ', "");
                    bookPrice[i] = prompt('Enter the Price: ',0);
            }
            for(var i = 0; i < arraySize; i++)
            {
                outMsg = outMsg + bookName[i] + " " + bookPrice[i] + "</br>" + total; // put backslash n ('n) to show on window
                //alert(outMsg)
                document.getElementById("Books").innerHTML = outMsg;
            }
    }
    </script>
</body>
<html>

在循环中,您需要对total变量进行计数,如下所示:

for(var i = 0; i < arraySize; i++)
{
    total += parseFloat(bookPrice[i]);
    // Whatever else you need to do...
}

计算total并使用parseInt(或parseFloat)键入

    function Books() {
      //define variables 
      var arraySize = 2;
      var bookName = new Array(arraySize);
      var bookPrice = new Array(arraySize);
      var total = 0;
      var outMsg = "";
      for (var i = 0; i < arraySize; i++)
      {
        bookName[i] = prompt('Enter Book Name:  ', "");
        bookPrice[i] = prompt('Enter the Price: ', 0);
        total += parseInt(bookPrice[i]);
      }
      for (var i = 0; i < arraySize; i++)
      {
        outMsg = outMsg + bookName[i] + " " + bookPrice[i] + "</br>" + total + "</br>"; // put backslash n ('n) to show on window
        //alert(outMsg)
        document.getElementById("Books").innerHTML = outMsg;
      }
    }
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Array 02</title>
</head>
<form action="">
  <p>BOOK SALE</p>
  <p id="Books" </p>
    <input type="button" value="Start" onClick="Books()" ;>
</form>
<body>
  <script>
  </script>
</body>
<html>

首先,您需要使用parseInt()并将其添加到for循环中的total变量中来计算合计。然后,您只需要计算要附加的响应,然后将第二个for循环之外的响应附加到所需的元素。阵列02

  <form action="">
<p>BOOK SALE</p>
<p id="Books"</p>
<input type="button" value="Start" onClick="Books();" >
</form>  
<body>
<script>
    function Books()
    {
            //define variables 
            var  arraySize = 2;
            var bookName = new Array(arraySize);
            var bookPrice = new Array(arraySize);
            var total = 0;
            var outMsg      = "";
            for(var i = 0; i < arraySize; i++)    
            {
                    bookName[i] = prompt('Enter Book Name:  ', "");
                    bookPrice[i] = prompt('Enter the Price: ',0);
                    total += parseInt(bookPrice[i]);
            }
            console.info("TOTAL PRICE:" + total );//this will print in blue color the total price in your console
            for(var i = 0; i < arraySize; i++)    
            {
                outMsg = outMsg + bookName[i] + " " + bookPrice[i] + "</br>" + total; // put backslash n ('n) to show on window
                //alert(outMsg)
                console.info("OutMsg :" + outMsg);// use console.log(),console.info(), console.warn(), instead of the old school alert() along with the browser/firebug console.
                 //You should move this line out of for loop because you only have one element in which you wish to append your output
                //document.getElementById("Books").innerHTML = outMsg;
            }
            document.getElementById("Books").innerHTML = outMsg;
    }
    </script>
</body>
<html>