Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Assignment 3 Part 2 : Add a stopwatch to the Clock application In this assignment, you ll add a stopwatch feature to the application you
Assignment Part : Add a stopwatch to the Clock application
In this assignment, youll add a stopwatch feature to the application you created in the previous assignment. The stopwatch will display elapsed minutes, seconds, and milliseconds. The enhanced application looks like this:
Open the starter folder attached zipped to this Assignment folder:
EvaluationsAssignmentsAssignment AssignmentFiles.zip clockstopwatch
In the JavaScript file, note the $ displayCurrentTime padSingleDigit and DOMContentLoaded event handler functions from the Clock application. In addition, note the global variables and starting code for the tickStopwatch startStopwatch stopStopwatch and resetStopwatch functions.
In the tickStopwatch function, add code that adds milliseconds to the elapsedMilliseconds variable and then adjusts the elapsedMinutes and elapsedSeconds variables accordingly. Then, add code that displays the result in the appropriate span tags in the page.
In the startStopwatch function, add code that starts the stopwatch. Be sure to cancel the default action of the link too.
In the stopStopwatch and resetStopwatch functions, add code that stops the stopwatch. Also, in the resetStopwatch function, reset the elapsed time and the page display. Be sure to cancel the default action of the links too.
In the DOMContentLoaded event handler, attach the stopwatch event handlers to the appropriate links.
In index.html change the text in the tag to your name and student number. "use strict";
const $ selector document.querySelectorselector;
const padSingleDigit num num.toStringpadStart;
const displayCurrentTime
const now new Date;
const hours now.getHours;
let ampm AM; set default value
correct hours and AMPM value for display
if hours convert from military time
hours hours ;
ampm PM;
else adjust noon and midnight
switch hours
case : noon
ampm PM;
break;
case : midnight
hours ;
ampm AM;
$#hours"textContent hours;
$#minutes"textContent padSingleDigitnowgetMinutes;
$#seconds"textContent padSingleDigitnowgetSeconds;
$#ampm"textContent ampm;
;
global stop watch timer variable and elapsed time object
let stopwatchTimer null;
let elapsedMinutes ;
let elapsedSeconds ;
let elapsedMilliseconds ;
const tickStopwatch
increment milliseconds by milliseconds
if milliseconds total increment seconds by one and reset milliseconds to zero
if seconds total increment minutes by one and reset seconds to zero
display new stopwatch time
;
event handler functions
const startStopwatch evt
prevent default action of link
do first tick of stop watch and then set interval timer to tick
stop watch every milliseconds. Store timer object in stopwatchTimer
variable so next two functions can stop timer.
;
const stopStopwatch evt
prevent default action of link
stop timer
;
const resetStopwatch evt
prevent default action of link
stop timer
reset elapsed variables and clear stopwatch display
;
document.addEventListenerDOMContentLoaded
set initial clock display and then set interval timer to display
new time every second. Don't store timer object because it
won't be needed clock will just run.
displayCurrentTime;
setIntervaldisplayCurrentTime;
set up stopwatch event handlers
;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started