Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. At the start of the JavaScript file, you’ll see the declarations for two arra

ID: 3915191 • Letter: 2

Question

2. At the start of the JavaScript file, you’ll see the declarations for two arrays: one for names and one for scores, and each array contains four elements. You’ll also see the code for the $ function as well as an onload event handler that attaches three functions named addScore(), displayResults(), and displayScores() to the click events of the buttons.

3. Write the displayResults function(). It should derive the average score and the highest score from the arrays and then display the results in the div element with “results” as its id, as shown above. To display the results, you need to add nodes to the DOM with the heading as an h2 element and the average and highest scores as

elements. The easiest way to do that is to use the innerHTML property as shown in figure 6-13.

4. Write the displayScores() function. It should get the names and scores from the arrays and display them as rows in the HTML table element with “scores_table” as its id, as shown above.

5. Write the addScore() function. It should add a name and score to the two arrays. To test whether this works, you can click the Display Scores button and see if the new name and score have been added to the table.

6. If you haven’t already done it, add data validation to addScore() function. The Name entry must not be empty and the Score entry must be a positive number from 0 through 100. If either entry is invalid, use the alert() method to display this error message: “You must enter a name and a valid score”.

7. Make sure that your application moves the cursor to the Name field when the application starts and after a name and score have been added to the array.

Explanation / Answer

If you have any doubts, please give me comment...

scores.html

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Test Score Array</title>

<link rel="stylesheet" href="styles.css" />

<script src="test_scores.js"></script>

</head>

<body>

<main>

<h1>Use a Test Score array</h1>

<label for="name">Name:</label>

<input type="text" id="name"><br>

<label for="score">Score:</label>

<input type="text" id="score"><br>

<label>&nbsp;</label>

<input type="button" id="add" value="Add to Array">

<input type="button" id="display_results" value="Display Results">

<input type="button" id="display_scores" value="Display Scores"><br>

<h2>Results</h2>

<div id="results"></div>

<h2>Scores</h2>

<table id="scores_table"></table>

</main>

</body>

</html>

test_scores.js

var names = ["Ben", "Joel", "Judy", "Anne"];

var scores = [88, 98, 77, 88];

var $ = function(id) { return document.getElementById(id); };

function addScore() {

var name = $("name").value;

var score = $("score").value;

names.push(name);

scores.push(score);

}

function displayResults() {

var avg = 0.0,

highest = 0;

for (var i = 0; i < scores.length; i++) {

avg += parseFloat(scores[i]);

if (scores[highest] < scores[i])

highest = i;

}

avg = avg / scores.length;

$("results").innerHTML = "Average score = " + avg + "<br />";

$("results").innerHTML += "High Score = " + names[highest] + " with a score of " + scores[highest];

}

function displayScores() {

var str = "<table>";

str += "<tr align='left'><th>Name</th><th>Score</th></tr>";

for (var i = 0; i < scores.length; i++) {

str += "<tr><td>" + names[i] + "</td><td>" + scores[i] + "</td></tr>";

}

str += "</table>";

$("scores_table").innerHTML = str;

}

window.onload = function() {

$("add").onclick = addScore;

$("display_results").onclick = displayResults;

$("display_scores").onclick = displayScores;

};

styles.css

body {

font-family: Arial, Helvetica, sans-serif;

background-color: white;

margin: 0 auto;

padding: 10px 20px;

width: 600px;

border: 3px solid blue;

}

h1 {

color: blue;

margin-top: 0;

margin-bottom: .5em;

}

h2 {

color: blue;

font-size: 120%;

padding: 0;

margin-bottom: .5em;

}

main {

padding: 1em 2em;

}

label {

float: left;

width: 3em;

text-align: right;

}

input {

margin-left: 1em;

margin-bottom: .5em;

}

p {

margin: 0;

}

td {

width: 10em;

}