HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link name="favicon" type="image/x-icon" href="https://skillzam.com/uploads/system/e353194f3b7cd1b75d69c3ab88041985.png" rel="shortcut icon" />
<title>Decision Making</title>
</head>
<body>
<!--
"if" statement : used to specify a block of code to be executed, if a specified condition is true.
"else" statement : used to specify a block of code to be executed, if the same condition is false.
"else if" statement : used to specify a new condition to test, if the first condition is false.
SYNTAX:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
-->
<h1>Demo: Decision Making</h1>
<div>
<h2>if statement</h2>
<p id="para1"></p>
</div>
<div>
<h2>if / else statement </h2>
<p id="para2"></p>
</div>
<div>
<h2>if / else if / else statement - Example(1)</h2>
<p id="para3"></p>
</div>
<div>
<h2>if / else if / else statement - Example(2)</h2>
<p id="para4"></p>
</div>
<div>
<h2>Nested if / else statements</h2>
<p id="para5"></p>
</div>
<!-- JavaScript code Starts here -->
<script>
/************************************************/
// if statement
// Example: Find the number is EVEN or ODD
//
let randNum = Math.floor(Math.random() * 100); // Generate random number using Math.random()
let randMod = randNum % 2; // Check if even or odd
if (randMod === 0) {
document.getElementById('para1').innerHTML =
`The number ${randNum} is EVEN.`;
}
if (randMod !== 0) {
document.getElementById('para1').innerHTML =
`The number ${randNum} is ODD.`;
}
/************************************************/
// if / else statement
// Example : Find the number is EVEN or ODD
//
let ranNum = Math.floor(Math.random() * 100);
let ranMod = ranNum % 2;
if (ranMod === 0) {
document.getElementById('para2').innerHTML =
`The number ${ranNum} is EVEN.`;
} else {
document.getElementById('para2').innerHTML =
`The number ${ranNum} is ODD.`;
}
/************************************************/
// if / else if / else statement
// Example : Programming Language selection
//
const progLang = "JavaScript".toLowerCase();
if (progLang === 'javascript') {
document.getElementById('para3').innerHTML =
"I like JavaScript! Can create wonders...";
} else if (progLang === 'python') {
document.getElementById('para3').innerHTML =
"I love Python! DataScience loves it too.";
} else if (progLang === 'html') {
document.getElementById('para3').innerHTML =
"HTML! It is so fun.";
} else {
document.getElementById('para3').innerHTML =
"Oh! I am yet to learn it.";
}
/************************************************/
// if / else if / else statement
// Example : Rewarding good performing students
//
// 0-49 - Unsatisfactory ₹0
// 50 - 59 - Fair ₹6000
// 60 - 69 - Good ₹6000
// 70 - 79 - Very Good ₹8000
// 80 - 89 - Excellent ₹8000
// 90 - 100 - Outstanding ₹10000
//
const score = 67; // Input score - (Change here)
if (score < 50) {
document.getElementById('para4').innerHTML =
"Perform Well. Keep your spirits high!";
} else if (score < 60) {
document.getElementById('para4').innerHTML =
"You are rewarded ₹6000";
} else if (score < 70) {
document.getElementById('para4').innerHTML =
"You are rewarded ₹6000";
} else if (score < 80) {
document.getElementById('para4').innerHTML =
"You are rewarded ₹8000";
} else if (score < 90) {
document.getElementById('para4').innerHTML =
"You are rewarded ₹8000";
} else {
document.getElementById('para4').innerHTML =
"You are rewarded ₹10000";
}
/************************************************/
// Nested if / else statements
// Example : Wonderword
// Wonderword must be minimum 9 characters & cannot include SPACES
//
const wonderWord = "$killzam9";
if (wonderWord.length >= 9) {
if (wonderWord.indexOf(' ') === -1) {
document.getElementById('para5').innerHTML =
"Perfect Wonderword!";
} else {
document.getElementById('para5').innerHTML =
"Wonderword cannot contain SPACES!";
}
} else {
document.getElementById('para5').innerHTML =
"Wonderword is too short! Must be minimum 9 characters";
}
</script>
<!-- JavaScript code Ends here -->
</body>
</html>