Data Structure and Algorithm Works
Data Structure and Algorithm Works
Solution:
var number = 5;
if (number > 0) {
console.log("The number is positive.");
} else {
console.log("The number is not positive.");
}
Output:
1
Solution:
var number = 7;
if (number % 2 === 0) {
console.log("The number is even.");
} else {
console.log("The number is odd.");
}
Output:
Solution:
2
Output:
num2 is greater.
Solution:
Output:
Grade: B
3
Exercise #5 — Ticket Pricing
Solution:
Output:
Solution:
4
var year = 2024;
if (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
console.log(year + " is a leap year.");
} else {
console.log(year + " is not a leap year.");
}
Output:
Solution:
5
Output:
Discount: 20%
Solution:
Good afternoon!
6
Scenario: Write a program that calculates the Body Mass
Index (BMI) and categorizes it.
Solution:
Output:
BMI: 22.86
Category: Normal weight
7
Solution:
var secretNumber = 7;
var guess = 5; // The player's guess, change this to see that
different code lines are executed based on the conditions
if (guess === secretNumber) {
console.log("Congratulations! You guessed the correct number.");
} else if (guess < secretNumber) {
console.log("Try a higher number.");
} else {
console.log("Try a lower number.");
}