ICT-4570 Homework 3
What to Hand In
Canvas submission instructions:
Please combine multiple files into a single "zip" archive, and save it in a location that you will remember. When you are ready to submit the assignment solution, open the assignment in Canvas, and click on the Submit for Evaluation button at the top, attach the file, and click Submit for Evaluation at the bottom.
Canvas submission instructions:
- Politics and poker
Build a simple form containing: Inputs for at least two candidate names, candidate parties, an "as-of" date, and places for raw vote counts.
Also put on the form two output locations for total votes, and percentages for each candidate, which will be reported with 1 decimal place.
The form should also have a button entitled Calculate which will tie to a function to read the inputs and place the output results.
Unparsable dates and other errors should be reported or corrected upon pressing the calculate button. See notes for detailed checks.
- A function to look up an HTML element by id and return the element
- A function which takes an Array of numbers as a parameter, and returns the sum.
- A function which takes an Array of numbers as a parameter, and returns an array of percentages (see notes for this computation).
- A function which formats a number into a percentage (see notes for this computation).
- A function to collect all the vote counts and return an Array, ordered by candidate, of their vote counts.
- A function to (a) collect all the counts; (b) get the sum of all votes; (c) compute the percentages for each candidate; and (d) place that result on the form in the correct locations.
- A function to initialize the form by setting the date to the current date, and attaching the click action to the click button.
Please ensure the user sees error text in red
Be sure to handle the following situations explicitly (comment if no code is required):
- Input is blank
- Number input is not a number
- Number input is zero or negative
- Date is not parseable (see note below)
If you post errors, warnings, or log messages to the console, include the console in the screenshot.
Some browsers enable the input type to be selected to prevent illegal values from being selected. You are welcome to use these, but please remember that browsers are NOT obligated to support these, and some will just treat them as input text fields (so you still need to code the checks).
Remember that getting elements by id, class, or tag name is pretty straightforward.
To compute a percentage from a vote count, simply divide the candidate's vote count by the total count
To format a percentage, multiply the number by 100, convert the number with toFixed(n), where n is replaced with the number of decimal places desired, and add the string "%".
Example of toFixed: (234.5).toFixed(2) will yield "234.50"
It might be called like this:
today = new Date(); isodate = toISODate(today);
Just for fun: If you add a button and attach a function that calls
print(), you can print your result (or save them to, say, a PDF if your dialog allows). Interestingly, the window.print specification is in the HTML specification document!
|HTML and CSS, to create the form||40|
|Screenshots of results, including error cases.||20|