Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JS File Open the vw_congminn.js file and study the contents. Note that the file contains the results of 8 congressional elections in Minnesota. The candidate

JS File

Open the vw_congminn.js file and study the contents. Note that the file contains the results of 8 congressional elections in Minnesota. The candidate information is stored in multidimensional arrays named candidate, party, and votes. Do not make any changes to this file.

Variables

Next, go to the vw_results.js file. Declare a variable named reportHTML containing the following HTML text

title

where title is the value of the raceTitle variable stored in the vw_congminn.js file.

For Loop

Create a for loop that loops through the contents of the racearray using i as the counter variable. Place the commands specified in the following steps within this program for loop:

Create a variable named totalVotes that will store the total votes cast in each race. Set its initial value to 0.

Calculate the total votes cast in the current race by applying the forEach() method to i index of the votes array using the calcSum() function as the callback function.

Add the following HTML text to the value of the reportHTML variable to write the name of the current race in the program loop

where race is the i index of the race array.

Call the candidateRows() function (you will create this function shortly) using the counter variable i and the totalVotes variable as parameter values. Add the value returned by this function to the value of the reportHTMLvariable.

Add the text

race
CandidateVotes
to the value of the reportHTMLvariable.

After the for loop has completed, write the value of the reportHTML variable into the innerHTML of the first (and only) section element in the document.

JS File Cont.

Rows Function

Next, create the candidateRows() function. The purpose of this function is to write individual table rows for each candidate, showing the candidates name, party affiliation, vote total, and vote percentage. The candidateRows() function has two parameters named raceNum and totalVotes. Place the commands in the following steps within this function.

Declare a local variable named rowHTML that will contain the HTML code for the table row. Set the initial value of this variable to an empty text string.

Create a for loop in which the counter variablej goes from 0 to 2 in steps of 1 unit. Within the for loop do the following:

Declare a variable named candidateName that retrieves the name of the current candidate and the current race. (Hint : Retrieve the candidate name from the multidimensional candidate array using the reference, candidate[raceNum][j].)

Declare a variable named candidateParty that retrieves the party affiliation of the current candidate in the current race from the multidimensional party array.

Declare a variable named candidateVotes that retrieves the votes cast for the current candidate in the current race from the multidimensional votes array.

Declare a variable named candidatePercent equal to the value returned by the calcPercent() function, calculating the percentage of votes received by the current candidate in the loop. Use candidateVotes as the first parameter value and totalVotes as the second parameter value.

Add the following HTML code to the value of the rowHTML variable

  name ( party )  votes ( percent )  

where name is the value of candidateName, party is the value of candidateParty, votes is the value of candidateVotes, and percent is the value of candidatePercent. Apply the toLocaleString() method to votes in order to display the vote total with a thousands separator. Apply the toFixed(1)method to percent in order to display percentage values to 1 decimal place.

Return the value of the rowHTML variable.

HTML File Cont.

Open vw_election.html Verify that the three candidate names, party affiliations, votes, and vote percentages are shown for each of the eight congressional races.

Bar Chart Function

Pam also wants the report to display the vote percentages as bar charts with the length of the bar corresponding to the percentage value. Return to the vw_results.js file. At the bottom of the file create a function named createBar() with one parameter named partyType. Add the commands described in the following steps to the function:

Declare a variable named barHTML and set its initial value to an empty text string.

Create a switch/case statement that tests the value of the partyType parameter.

If partyType equal D set barHTML equal to:

 

If partyType equals R set barHTML equal to:

 

Finally, if partyType equals I set barHTML to:

 

Return the value of barHTML. Next, add these empty data cells to the race results table, with one cell for every percentage point cast for the candidate.

Bar Chart Creation

Scroll up to the candidateRows() function. Directly before the line that adds the HTML code to the value of the rowHTML variable, insert a for loop with a counter variable k that goes from 0 up to a value less than candidatePercent in increments of 1 unit. Each time through the loop call the createBar() function using candidateParty as the parameter value.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions