Write down what you wish to accomplish. Then break it down into smaller parts. Try to find the simplest part, and implement just that part. If you can’t get that simple part to work, see if you can break it down into even simpler parts.
If you can’t break it down any further, move on to the next problem. Perhaps by the time you finish the problem you can solve, you’ll have some insight into the related problem that you couldn’t.
Learn to love your browser’s reload button. Whenever you make a change that might affect your web page, test immediately. Don’t make a whole bunch of changes before testing; if your web page no longer works, you won’t necessarily know which change caused it to fail. Write your programs a step at a time and test often.
Test things that you don’t even expect to happen: try to think up weird scenarios and come up with a means of testing what happens to your page under those scenarios. This will give you valuable information about what your code is really doing.
Also, make extensive use of the “window.alert()” function to let you know the value of variables at troublesome points.
You should almost never repeat the same basic code more than once. If you need the same routine a second time, seriously consider moving it into a subroutine.
For example, if you wanted to put the date into your “Current Time:” line, we already have the code that creates a date. Unfortunately, if you call it directly you’ll end up with “Today” as the string, because that’s what that subroutine does. Rather than duplicating the portion of the code that creates the actual date, move it out of the “formatDate()” subroutine. Call it something like “makeDate()”. Then, you can call that subroutine both from “formateTime()” and from “formatDate()”.
Another example might be the code we used for prepending a zero to numbers that are smaller than 10. We probably should have created a subroutine that would do that for us, and then call that subroutine every time we need to prepend zeros.
But consider the things you might want to do with a subroutine at the moment you create it. Might we want to prepend something other than zeros? Might we want to pad to three or even four characters instead of just to two? An appropriate subroutine might be called with “thishour = leftpad(thishour, "0", 2);” to ensure that “thishour” has at least two characters, and prepend a zero if it does not. One could then use “indexline = leftpad(pagenumber, ".", 4);” to prepend periods and ensure four characters in the “indexline” string. The function might look something like:
function leftpad(theItem, thePad, minSize) {
newItem = new String(theItem);
while (newItem.length < minSize) {
newItem = thePad + newItem;
}
return newItem;
}
Once you have a hammer, all problems start to look like nails. It can be easy, once you start programming JavaScript, to look at every web page problem as a problem with a JavaScript solution. Look for simpler HTML solutions first. HTML is faster and more reliable than JavaScript, and will last longer.
Some problems will have to be solved with server-side programming, either using server-side includes or server-side programming such as with Perl or PHP. Trying to solve these problems with client-side JavaScript is prone to errors, sometimes dangerous ones if money or privacy is involved.