1. Open this HTML file: exercises_short\\ch02\\test_scores.html Then, run the ap
ID: 3550802 • Letter: 1
Question
1. Open this HTML file:
exercises_shortch02 est_scores.html
Then, run the application to refresh your memory about how it works.
2. Declare a variable named bestScore and assign it a starting value of zero.
3. For each valid user entry, compare the entry with the value in bestScore. If the user entry is greater than the value in bestScore, set the bestScore variable equal to the user entry, because it is now the best score.
4. Add to the alert statement at the end of this application so it displays the best score in the dialog box as shown above.
Here is my code: where am I going wrong?
<!DOCTYPE html>
<html>
<head>
<title>Average Test Scores</title>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script>
var total = 0;
var entryCount = 0;
var entry;
var bestScore = 0;
do {
entry = prompt("Enter test score " +
"Or enter 999 to end entries", 999);
entry = parseInt(entry);
if (entry >= 0 && entry <= 100) {
total = total + entry;
entryCount++;
}
if (entry > bestScore) {
bestScore = entry;
}
else if (entry != 999){
alert("Entry must by a valid number from 0 through 100 " +
"Or enter 999 to end entries");
}
}
while (entry != 999);
var average = total/entryCount;
average = parseInt(average);
alert("Average score is" + average/n" +
"Best Score is" = bestScore");
</script>
</head>
<body>
<section>
<h1>This page is displayed after the JavaScript is executed</h1>
</section>
<p> </p>
<p> </p>
</body>
</html>
Explanation / Answer
<!-- formatted code at https://gist.github.com/bitwiser/9139556 -->
<!DOCTYPE html>
<html>
<head>
<title>Average Test Scores</title>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script>
var total = 0;
var entryCount = 0;
var entry;
var bestScore = 0;
do {
entry = prompt("Enter test score " +
"Or enter 999 to end entries", 999);
entry = parseInt(entry);
if (entry >= 0 && entry <= 100) {
total = total + entry;
entryCount++;
}
if (entry!=999 && entry > bestScore) {
bestScore = entry;
}
else if (entry != 999){
alert("Entry must by a valid number from 0 through 100 " +
"Or enter 999 to end entries");
}
}
while (entry != 999);
var average = total/entryCount;
average = parseInt(average);
alert("Average score is" + average +"Best Score is = "+bestScore);
</script>
</head>
<body>
<section>
<h1>This page is displayed after the JavaScript is executed</h1>
</section>
<p> </p>
<p> </p>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.