I am dealing with the question of:
Create a Java Script control structure using if..else statement. Declare one variable and use Boolean condition to determine whether the condition is true or false. If the first condition evaluates to true, then the program will run the first block of code. Otherwise, it will run the else block. Show your output using the given condition.
{
if weather is equal to thunderstorm;
then display “Stay at home”;
else
then display “Happy Journey”;
}
Is it possible to show the output in html? How can I fix my code to show the output? It is not working currently.
This is my code:
<body>
<p id="demo"></p>
<script>
boolean thunderstorm = true;
if (thunderstorm) {
document.getElementById("demo").innerHTML = "Stay at home";
} else {
document.getElementById("demo").innerHTML = "Happy Journey";
}
</script>
</body>
Welcome to SO :)
If you check your developer tools and look at the console, you should see an error like "message": "Uncaught SyntaxError: Unexpected identifier",. In this case that is coming from your usage of "boolean". In JavaScript we don't have to declare variable types like that, instead you would use const, let or var to declare a variable, like on my example below.
const thunderstorm = true;
if (thunderstorm){
document.getElementById("demo").innerHTML ="Stay at home";
} else {
document.getElementById("demo").innerHTML ="Happy Journey";
}
<p id="demo"></p>
With JavaScript the type of a variable is not as strict as in some other languages. To quote MDN:
JavaScript is a loosely typed and dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures