JavaScript · Syntax · Beginner
Conditions
Practical exercises on conditional statements in JavaScript: comparing numbers, checking ranges, working with strings, passwords, percentages, and basic if-else logic.
Quick topic start and explanations before exercises (exercises below):
Comparing values and combining conditions
#Common patterns with conditions
#Exercises:
Checking the sign of a number.
#let number = 5;
Solution
let number = 5;
if (number > 0) {
console.log("The number is positive");
} else if (number < 0) {
console.log("The number is negative");
} else {
console.log("The number is zero");
}
// or you can do this: first prepare the message, and then print it once
let number = 5;
let result;
if (number > 0) {
result = "The number is positive";
} else if (number < 0) {
result = "The number is negative";
} else {
result = "The number is zero";
}
console.log(result);
Age category.
#let age = 16;
Solution
let age = 16;
if (age <= 12) {
console.log("Child");
} else if (age <= 17) {
console.log("Teenager");
} else {
console.log("Adult");
}
// second option: boundaries can be written explicitly
let age = 14;
if (age < 13) {
console.log("Child");
} else if (age >= 13 && age <= 17) {
console.log("Teenager");
} else {
console.log("Adult");
}
The larger of two numbers.
#let a = 10; let b = 7;
Solution
let a = 10;
let b = 7;
if (a > b) {
console.log("Larger number:", a);
} else if (b > a) {
console.log("Larger number:", b);
} else {
console.log("The numbers are equal");
}
// or you can do this using the result variable
let a = 10;
let b = 5;
let result;
if (a === b) {
result = "The numbers are equal";
} else if (a > b) {
result = `Larger number: ${a}`;
} else {
result = `Larger number: ${b}`;
}
console.log(result);
Temperature: cold, warm, hot.
#let temp = 20;
Solution
let temp = 20;
if (temp < 10) {
console.log("Cold");
} else if (temp <= 24) {
console.log("Warm");
} else {
console.log("Hot");
}
Checking if a number is even.
#let num = 5;
Solution
let num = 5;
if (num % 2 === 0) {
console.log("The number is even");
} else {
console.log("The number is odd");
}
// or you can do this: save the check in a separate variable
let num = 8;
let isEven = num % 2 === 0;
if (isEven) {
console.log("The number is even");
} else {
console.log("The number is odd");
}
Checking password length.
#let password = "javascript";
Solution
let password = "javascript";
if (password.length < 6) {
console.log("Password is too short");
} else {
console.log("Password accepted");
}
Grade on the A-F scale.
#let score = 85;
Solution
let score = 85;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else if (score >= 70) {
console.log("C");
} else if (score >= 60) {
console.log("D");
} else {
console.log("F");
}
// or you can do this: move from smaller boundaries to larger ones
let score = 85;
if (score < 60) {
console.log("F");
} else if (score < 70) {
console.log("D");
} else if (score < 80) {
console.log("C");
} else if (score < 90) {
console.log("B");
} else {
console.log("A");
}
Divisibility of two numbers.
#let a = 10; let b = 7;
Solution
let a = 10;
let b = 7;
if (b === 0) {
console.log("Division by zero is not allowed");
} else if (a % b === 0) {
console.log("The first number is divisible by the second");
} else {
console.log("The first number is not divisible by the second");
}
Rounding within the range 0-100.
#let num = 5;
Solution
let num = 5;
if ((num < 0) || (num > 100)) {
console.log("Only from 0 to 100");
} else {
console.log(Number(num.toFixed(2)));
}
// or you can do this: store the range check in a variable
let num = 42.678;
let inRange = num >= 0 && num <= 100;
if (inRange) {
console.log(Number(num.toFixed(2)));
} else {
console.log("Only from 0 to 100");
}
Long or short string.
#let text = "JavaScript course";
Solution
let text = "JavaScript course";
if (text.length > 10) {
console.log("Long string");
} else {
console.log("Short string");
}
// or you can do this using the message variable
let text = "JavaScript";
let message;
if (text.length > 10) {
message = "Long string";
} else {
message = "Short string";
}
console.log(message);
Percentage of a number.
#let a = 10; let b = 7;
Solution
let a = 10;
let b = 7;
if (b === 0) {
console.log("Division by zero is not allowed! And there is no part of zero.");
} else {
let result = a / b * 100;
result = Number(result.toFixed(2));
console.log(String(result) + "%");
}
Rounding to the required decimal place.
#let num = 5; let rounding = 2;
Solution
let num = 5;
let rounding = 2;
if (rounding < 0) {
console.log("The value for the number of decimal places cannot be less than 0.");
} else {
console.log(Number(num.toFixed(rounding)));
}
Converting a string to number.
#let num = "";
Solution
let num = "5.25";
if (num.includes(".")) {
console.log("The string contains a dot");
} else {
console.log("There is no dot");
}
num = Number(num);
console.log(num);
Division without zero error.
#let a = 10; let b = 7;
Solution
let a = 10;
let b = 7;
if (b === 0) {
console.log("Division by zero is not allowed! Even if you really want to!");
} else {
console.log("Result:", a / b);
}
First name and last name form.
#let name = "Alex"; let last_name = "Smith";
Solution
let name = "Alex";
let lastName = "Smith";
if (name.includes(" ")) {
console.log("Fill out the form carefully!! That was the last blank form hahaha)))");
} else {
let fullName = name + " " + lastName;
console.log(fullName);
}