JavaScript: Basic Programming
Topics
|
|
Online Course: Foundations of Programming: Fundamentals
Take the course "Foundations of Programming: Fundamentals" with Simon Allardice provided by Northeastern University through our Lynda.com 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 Lynda via Blackboard. The entire course is about 4:47 hours, although modules 12, 13, and 14 are less important and can be skipped which reduces the course time by about an hour. The estimated course completion time does not include time spent in the programming exercises. It is strongly recommended that the exercises be completed as programming is best learned through practice.
By the end of this course you will be able to:
By the end of this course you will be able to:
- Create basic programming structures: loops, selection, functions
- Learn essentials of JavaScript
- Differentiate weakly and strongly typing
- Structure source code with comments and whitespace
- Debug faulty programs
- Set up basic algorithms
- Build modular code
- Work with collections, arrays, and strings
- Use classes and objects
Online Course: JavaScript Essentials
Take the course "JavaScript Essentials" with Simon Allardice provided by Northeastern University through our Lynda.com 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 Lynda via Blackboard. The entire course is about 5:31 hours. The estimated course completion time does not include time spent in the programming exercises. It is strongly recommended that the exercises be completed as programming is best learned through practice.
By the end of this course you will be able to:
By the end of this course you will be able to:
- Understanding the structure of JavaScript code
- Creating variables, functions, and loops
- Writing conditional code
- Sending messages to the console
- Working with different variable types and objects
- Creating and changing DOM objects
- Event handling
- Working with timers
- Debugging JavaScript
- Building smarter forms
- Working with CSS, HTML5, and JavaScript
- Using regular expressions
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.