0

I'm having trouble with a JavaScript timed-output that states some type of greeting and then the time. Code:

<script language="Javascript">
<!--
{
    var now = new Date();
    var h = now.getHours();
    var m = now.getMinutes();
    var s = now.getSeconds();
    if (hours < 7){timemsg = "Wakey wakey, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds};
    if (hours > 6 && hours <12){timemsg = "Good morning, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    if (hours > 11 && hours <18){timemsg = "Good afternoon, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    if (hours >17){timemsg = "Good evening, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    document.write(timemsg)};
// -->
</script>

The expected output should go something like this: Wakey Wakey,/Good morning/afternoon/evening, Mr. Person. The time is: (time).

3 Answers3

2

There are many unset variables in your code, the starting { and the ending } are both syntax errors, too.

Below code is corrected, just replace alert(); with document.write(); again.

 var now = new Date();
 var hours = now.getHours();
 var minutes = now.getMinutes();
 var seconds = now.getSeconds();
 if (hours < 7){timemsg = "Wakey wakey, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds}
 if (hours > 6 && hours <12){timemsg = "Good morning, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds}
 if (hours > 11 && hours <18){timemsg = "Good afternoon, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds}
 if (hours >17){timemsg = "Good evening, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds}
 alert(timemsg);

Also, you did this quite often so I think it's worth mentioning. This:

The time is: " hours + ':'

Needs to be

The time is: " + hours + ':'

see the + before and after the variable. You need to add the + before and after a variable in a string.


SIDENOTE:

Part of ecmascript 6 are template strings. This is a good example of when to use them. For example:

"Good afternoon, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds

Can now be written as

`Good afternoon, Mr. Person. The time is: ${hours}:${minutes}:${seconds}`

which makes it way more readable. Note the supported browsers for this feature.

baao
  • 71,625
  • 17
  • 143
  • 203
0

After a couple more whiles, i was able to figure this out:

function startTime() {
    // Clock
    var hello="The time is: ";
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    h = checkTime(h);
    m = checkTime(m);
    s = checkTime(s);

    // Timed Greeting
    if (h == 6 && m == 30 && s == 0){window.alert("Go to school, dude!")};
    if (h < 7){timemsg = "You're up <B>really</B> early, Mr. Lawrence! "};
    if (h > 6 && h <12){timemsg = "Good morning, Mr. Lawrence! "};
    if (h > 11 && h <18){timemsg = "Good afternoon, Mr. Lawrence! "};
    if (h >17){timemsg = "Good evening, Mr. Lawrence! "};
    document.getElementById('txt').innerHTML = timemsg+hello+h+":"+m+":"+s;
    var t = setTimeout(function(){startTime()},500);
}

function checkTime(i) {
    if (i<10) {i = "0" + i};  // Add zero in front of numbers < 10
    return i;
}
-1

Try this:

<script language="Javascript">
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    if (hours < 7){timemsg = "Wakey wakey, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds};
    if (hours > 6 && hours <12){timemsg = "Good morning, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    if (hours > 11 && hours <18){timemsg = "Good afternoon, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    if (hours >17){timemsg = "Good evening, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    document.write(timemsg)};
</script>

It should work.

Anders
  • 8,307
  • 9
  • 56
  • 88