Module 4 pt 1

<h3>Fixed JavaScript Example</h3> <p id=”demo”>Display the result here.</p> <script> var greeting; var hour = new Date().getHours(); if (hour < 18) { greeting = “Good day”; } else { greeting = “Good evening”; } document.getElementById(“demo”).innerHTML = greeting; </script>

The original example contained an error in the placement of the closing curly bracket. The if statement needed to be closed before beginning the else statement. After correcting the bracket placement and running the code, the script successfully displayed either “Good day” or “Good evening” depending on the current time.

Leave a comment