1.我在代码的第一个注释中包含了我想要组合的两行。如果你能指出我需要做的事情,那真的会有所帮助。我刚刚开始使用javascript。如果你能推荐任何书籍,请告诉我。我正在阅读murach系列书籍,它真的很有帮助。
/*The two statements i want to combine are
entry = parseInt(entry);
var score1 = entry; */
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Average Test Scores</title>
<script>
var entry;
var average;
var total = 0;
//get 3 scores from user and add them together
entry = prompt("Enter test score");
entry = parseInt(entry);
var score1 = entry;
total = total + score1;
entry = prompt("Enter test score");
entry = parseInt(entry);
var score2;
total = total + score2;
entry = prompt("Enter test score");
entry = parseInt(entry);
var score3 = entry;
total = total + score3;
//calculate the average
average = parseInt(total/3);
</script>
</head>
<body>
<script>
document.write("<h1>The Test Scores App</h1>");
document.write("Score 1 = " + score1 + "<br>" +
"Score 2 = " + score2 + "<br>" +
"Score 3 = " + score3 + "<br><br>" +
"Average score = " + average + "<br><br>");
</script>
Thanks for using the Test Scores application!
</body>
</html>
答案 0 :(得分:0)
Addition assignment
, addition assignment
运算符将right operand
的值添加到变量中,并将结果赋给变量。
Unary plus (+)
, unary plus operator
位于其操作数之前,并计算其操作数,但尝试将其转换为number
,如果尚未的话。
简化版:
var total = 0;
var score1 = +prompt("Enter test score"); //Cast it to Number
total += score1; //Add it to total
var score2 = +prompt("Enter test score");
total += score2;
var score3 = +prompt("Enter test score");
total += score3;
var average = parseInt(total / 3);
document.write("<h1>The Test Scores App</h1>");
document.write("Score 1 = " + score1 + "<br>" +
"Score 2 = " + score2 + "<br>" +
"Score 3 = " + score3 + "<br><br>" +
"Average score = " + average + "<br><br>");