1

I recently started working with Javascript, like a few hours ago, and I can't figure out why it's still showing text it shouldn't be on my website. Dreamweaver says no error, but there has to be..

<script type="text/javascript">
     var day = new Date();
     var hr = day.getHours();
     if((hr == 1) || (hr == 2) || (hr == 3) || (hr == 4) || (hr == 5) || (hr == 6) || (hr == 7) || (hr == 8) || (hr == 9)); {
         document.write("Paragraph one stack example");
     }
    if(hr == 10) {
        document.write("P2 stack ex");
    }
    if((hr == 11) || (hr == 12) || (hr == 13)); {
        document.write("P3 stack ex.");
    }

</script>
1
  • 1
    Also, please shorten your if statement to if (hr >= 1 && hr <= 9). Commented Feb 25, 2015 at 3:11

2 Answers 2

1

Code is valid, but you have done mistake when put

;

at the end of if expression

if you remove this all will works fine!

var day = new Date();
var hr = day.getHours();
if((hr == 1) || (hr == 2) || (hr == 3) || (hr == 4) || (hr == 5) || (hr == 6) || (hr == 7) || (hr == 8) || (hr == 9)) {
  document.write("Paragraph one stack example");
}
if(hr == 10) {
  document.write("P2 stack ex");
}
if((hr == 11) || (hr == 12) || (hr == 13)) {
  document.write("P3 stack ex.");
}

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

Comments

1

From your code, slightly reformatted:

if((hr == 1) || ... || (hr == 9)); {
    document.write("Paragraph one stack example");
}

Get rid of that semicolon, it's making the entire if bit irrelevant.

What it translates to is:

if((hr == 1) || ... || (hr == 9))
    ;
{
    document.write("Paragraph one stack example");
}

In other words,

  • if hr is 1-9, do nothing.
  • regardless of the value of hr, output "Paragraph one stack example".

You have the same problem with the if statement for 11/12/13 as well.


A better solution for that "one through nine" if statement, by the way, would be:

if((hr >= 1) && (hr <= 9)) {
    document.write("Paragraph one stack example");
}

and you can further clean up the code since all the conditions are mutually exclusive:

<script type="text/javascript">
    var day = new Date();
    var hr = day.getHours();
    if ((hr >= 1) || (hr <= 9)) {
        document.write("Paragraph one stack example");
    } else if (hr == 10) {
        document.write("P2 stack ex");
    } else if ((hr >= 11) && (hr <= 13)) {
        document.write("P3 stack ex.");
    }
</script>

There's little point checking if hr is equal to 10 if you've already established that it's 4, for example. So you can use else if for that.

2 Comments

You have a semicolon after the if statement, that stops it in its tracks. Remove this and I'm sure it will work.
Sorry it's early here haven't woken up...I put my answer in the wrong place, regards

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.