JavaScript: Basic Programming
Topics
|
Online Course: Foundations of Programming: Fundamentals
Take the course
"
Programming Foundations: Fundamentals" with Annyce Davis provided by Northeastern University through
our LinkedIn Learning subscription. This course, while optional is strongly recommended
particularly for those students who have little to no prior experience in programming and require a more
fundamental introduction to programming before proceeding with embedding JavaScript in HTML.
Use your NU credentials to log on through the link or go to
Northeastern’s LinkedIn Learning.
Online Course: JavaScript Essentials
Take the course
"
JavaScript Essential Training" with Morten Rand-Hendriksen provided by Northeastern University
through our LinkedIn Learning subscription. This course, while optional is strongly recommended
to those students who wish to deepen their understanding of JavaScript. Use your NU credentials to log on
through the link or go to
Northeastern’s LinkedIn Learning.
Suggested Readings
Practice Exercise
James is trying to place some JavaScript on his
website that alerts clients as to when their help desk ticket will be answered. Tickets are addressed on a
first-come first-served basis. Each ticket has a place in the queue. For example, today the ticket might be
number 562. That means there are 561 other requests ahead. If we know the initial queue position and the
date on which is was entered we could use a simple linear forecasting model to estimate when the ticket will
be addressed. Let's say the ticket started out on the morning of 7/5 at position 781 and on the evening of
7/12 it's at position 201 that would mean that on average 73 tickets are processed each day (here's the
math: 781 - 201 = 580 tickets processed; 7/12 - 7/5 = 7 days; add one extra day as 7/5 was a full day, so 8
days; 580 / 8 = 72.5 or 73 rounded up). So, with that, we'd expect that the remaining 201 tickets would take
about 3 days to process: 201 / 73 = 2.7. Write a JavaScript program to do that calculation. Allow the user
to input a starting date and queue position and the current position. Use today() to get today's date.
Here's some useful code:
var date1 = new Date("7/11/2010");
var date2 = new Date("12/12/2010");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);
Observe that we need to enclose the date in quotes. The rest of the code gets the time difference in milliseconds and then divides to get the number of days.
A more correct solution (when dealing with time-zones)
Most of the solutions here don't take into account a case that fails when the two dates involved go
across a daylight saving change. In this case, the date on which day light saving change happens will
have a duration in milliseconds which != 1000*60*60*24, so the typical calculation will fail.
A more accurate way to get the number of days between two javascript dates can be written as follows:
var _MS_PER_DAY = 1000 * 60 * 60 * 24;
// a and b are javascript Date objects
function dateDiffInDays(a, b) {
// Discard the time and time-zone information.
var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
This works because UTC time never observes DST.