HTML and CSS Reference
In-Depth Information
If
partyType
equals
G
,
barText
should equal the following:
<td class='green'> </td>
Finally, if
partyType
equals
L
,
barText
should equal the following:
<td class='lib'> </td>
Be sure to add a
break
statement after each
case
statement so browsers do not
attempt to test additional cases after finding a match.
b. Create a
for
loop in which the counter variable goes from 1 up through the value
of the
percent
parameter in increments of 1. At each iteration, write the value of
the
barText
variable to the Web document.
6. Insert a function named
showresults()
. The purpose of this function is to show the
results of a particular race. The function has four parameters:
race
,
name
,
party
, and
votes
. The
race
parameter will contain the name of the race. The
name
parameter
will contain the array of candidate names. The
party
parameter will contain the array
of party affiliations. Finally, the
votes
parameter will contain the array of votes for
each candidate in the race. Add the following commands to the function:
a. Declare a variable named
totalv
equal to the value returned by the totalVotes()
function using
votes
as the parameter value.
b. Write the HTML code
<h2>
race
</h2>
<table>
<tr>
<th>Candidate</th>
<th class='num'>Votes</th>
<th class='num'>%</th>
</tr>
to the document, where
race
is the value of the
race
parameter.
7. Next, within the showResults() function, add a
for
loop in which the counter vari-
able starts at 0 and, while the counter is less than the length of the
name
array,
increase the counter in increments of 1. At each iteration of the
for
loop, run the
commands outlined in the following five steps:
a. Write the HTML code
<tr>
<td>
name
(
party
)</td>
<td class='num'>
votes
</td>
where
name
,
party
, and
votes
are the entries in the
name
,
party
, and
votes
arrays, respectively, for the index indicated by the counter variable.
b. Create a variable named
percent
equal to the value returned by the calcPercent()
function. Use the current value from the
votes
array for the value of the
item
parameter, and use the value from
totalV
for the value of the
sum
parameter.
c. Write the HTML code
<td class='num'>(
percent
%)</td>
where
percent
is the value of the
percent
variable.
d. Call the createBar()
function using the current value of the
party
array and
percent
as the parameter values.
e. Write a closing
</tr>
tag to the document.
8. After the
for
loop has completed within the showResults() function, write a closing
</table>
tag to the document.
9. Write comments throughout your code to document your work.