0

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];
    }
5
  • You're missing vars and arrau literals. Commented Jan 27, 2013 at 20:23
  • 6
    What is your question? Commented Jan 27, 2013 at 20:27
  • I believe it's fine because JavaScript Objects don't necessarily keep a cross-browser property ordering. So, you still need the weekday array for mapping. A bit of better formatting, code clean-up and usage of [] notation for array creation may have been better. Commented Jan 27, 2013 at 20:28
  • You can't access a JSON object property by an index value: http://stackoverflow.com/a/4231336/674700. Commented Jan 27, 2013 at 20:50
  • 1
    @AlexFilipovici You are confusing terms here. There is no such thing like JSON Object. Javascript has either Objects or Arrays (or simple datatypes like strings, number or booleans). JSON means javascript object notation and is a datatransfer format which bases upon javascript's object notation und thus is easily integrated into JS because if you parse it, you have a valid JS Object you can work with. Commented Jan 28, 2013 at 9:47

2 Answers 2

0

Not 100% sure what you are trying to do. If you just want to change today element color need to modify change() as follows:

function change(day){
    var x = document.getElementById(day);       
    x.style.color = weekdayColor[day];
}
var d = new Date(),
    day = d.getDay(),
    a = weekday[day];

/* now call change passing day as argument*/
change(a);

DEMO: http://jsfiddle.net/rhHjn/1

Sign up to request clarification or add additional context in comments.

Comments

0

Easier would be to use an array of objects as following:

var weekday = [
   {name:"sunday",color:"red"},
   {name:"monday",color:"blue"},
   // and so on
]

Then you can do something like this:

d = new Date();
day = d.getDay();
weekday[day].name  // yields name
weekday[day].color // yields color

Alternatively you could write a prototype method:

Date.prototype.getDayName = function() {
return ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
       [this.getDay()];
}

and now

var d = new Date;
d.getDayName(); // this would actually give you the name of the day

2 Comments

I can't change the initial var
@Xeo You cannot access the keys of an object via Array Notation [i] because object keys are not ordered! Other than iterating over all properties of the object e.g. via a for loop you cannot tell, which keys it actually contains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.