Here is the array
var weekdayColor = {
sunday : 'red', // sunday
monday : 'blue', // monday
tuesday: 'white', // tuesday
wednesday: 'black',
thursday: 'green',
friday: 'yellow',
saturday: 'orange'
}
I want to be able to do something along the lines of weekcayColor[0] to get sunday:
Here is the JavaScript I wrote for an interview which was already turned in.. I know there is an easier way to do this. The first var weekdayColor CANNOT BE CHANGED; also weekdayColor.sunday returns red.
Perhaps I am using the wrong date method or accessing the var incorrectly?
var weekdayColor = {
sunday : 'red', // sunday
monday : 'blue', // monday
tuesday: 'white', // tuesday
wednesday: 'black', // wednessday
thursday: 'green', // thursday
friday: 'yellow', // friday
saturday: 'orange' // saturday
}
var weekday=new Array();
weekday[0]="sunday";
weekday[1]="monday";
weekday[2]="tuesday";
weekday[3]="wednesday";
weekday[4]="thursday";
weekday[5]="friday";
weekday[6]="saturday";
d = new Date;
day = d.getDay();
console.log(weekday[day]);
a = weekday[day];
function change(){
var x = document.getElementById("weekday");
x.innerHTML = a;
x.style.color = weekdayColor[a];
}
vars and arrau literals.weekdayarray for mapping. A bit of better formatting, code clean-up and usage of[]notation for array creation may have been better.