我试图找到用户在弹出窗口中输入的价格的总和并显示总数。我是否必须在输出下添加另一行? 我试图在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>
答案 0 :(得分:1)
在循环中,您需要计算total
变量,如下所示:
for(var i = 0; i < arraySize; i++)
{
total += parseFloat(bookPrice[i]);
// Whatever else you need to do...
}
答案 1 :(得分:0)
计算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>
答案 2 :(得分:0)
首先,您需要使用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>