I'm brand new to web design and development and have just finished writing my first code for a website I am tinkering with. It may be overcomplicated for the purpose, but I just wanted to see what someone thought about it.
var days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
var months = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var newDate = new Date();
var today = newDate.getDate();
var lastDigit = today % 10;
var ending;
function addTheEnding() {
"use strict";
if (lastDigit === 1) {
ending = "st";
} else {
if (lastDigit === 2) {
ending = "nd";
} else {
if (lastDigit === 3) {
ending = "rd";
} else {
ending = "th";
}
}
}
}
addTheEnding();
function updateDate() {
"use strict";
return (days[newDate.getDay()] + " " + months[newDate.getMonth()] + " " + today + ending);
}
var currentDate = document.createTextNode(updateDate());
document.getElementById("date").appendChild(currentDate);
Truth is, I'm terribly proud of this simple little bit of code. I certainly stole the lastDigit idea. I was going to use the || operator. Still, it took me surprisingly long to get this to work properly. I know it could have been done in a more direct manner, but I thought it might be a good idea to practice using the more precise methods I will need as the code gets more complicated. Any thoughts? Should I separate the addTheEnding function into a couple of functions to simplify the if nesting? Oh and what about this "use strict" deal? I have seen a little bit about it. I mean I'm new to this and if they think we should use it I don't mind learning to do it with it.


Sign In
Create Account

Back to top









