0

I feel stupid because I´m stuck with a basic. I have three set of classes that contains paragraphs and I want to change the background color of each one depending on day (with New Dat.getDay().

I dont know how to mix for loop and if statements for each set of classes correctly. I guess it´s something simple but I missing it!

function changecolor() {
  var d = new Date();
  var n = d.getDay();
  var weekda = document.getElementsByClassName('weekdays');
  var sat = document.getElementsByClassName('saturday');
  var dom = document.getElementsByClassName('sun-fer');
  for (var i = 0; i < weekda.length && i < sat.length && i < dom.length; i++)
    if (n > 0 || n < 6) {
      weekda[i].setAttribute("style", "background-color:#0091ea;color:white;");
    }
  else if (n == 6) {
    sat[i].setAttribute("style", "background-color:#0091ea;color:white;");
  } else {
    dom[i].setAttribute("style", "background-color:#0091ea;color:white;");
  }
}
}
changecolor();
3
  • 1
    seems n > 0 || n < 6 should be n > 0 && n < 6 ... because every number is either greater than zero OR less than 6 (|| is OR, && is AND) Commented Jun 27, 2017 at 3:26
  • multiple for-loop terminating conditions looks like trouble Commented Jun 27, 2017 at 3:30
  • Thans so much @JaromandaX ! :) Commented Jun 27, 2017 at 3:39

2 Answers 2

2

You need to group conditions. Read more about Operator precedence

 for (var i = 0;( (i < weekda.length) && (i < sat.length) && (i < dom.length)); i++){
   // your code

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

2 Comments

But...< already has higher precedence than &&, so what does adding parentheses change?
Good question. Is the answer that the parentheses make it easier for the human reader to visually parse the multiple condition (?)
1

Part of the problem may be you have background-color:#0091ea;color:white; for all three options. Therefore you may not be seeing any change.

Personally I would break this up a little to make it more flexible and a little easier to read (and maintain). For example:

function changecolor() {
    var d = new Date();
    var e = null;
    var s = null;
    switch(d.getDay()) {
        case 6:
            e = document.getElementsByClassName('saturday');
            s = "background-color:#0091ea;color:white;";
            break;
        case 0:
            e = document.getElementsByClassName('sun-fer');
            s = "background-color:#0091ea;color:green;";
            break;
        default:
            e = document.getElementsByClassName('weekdays');
            s = "background-color:#0091ea;color:blue;";
    }
    // now update the color
    updateItem(e,s);
}

function updateItem(e,s) {
    var i, max = e.length;
    for(i=0;i<max;i++) {
        e[i].setAttribute("style",s);
    }
}

Comments

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.