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.
#Write a program that takes a number and displays whether it is positive, negative, or zero.
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.
#The program takes the user's age and displays: "Child" (under 12), "Teenager" (13–17), "Adult" (18 and older).
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.
#The user enters two numbers. Display the larger one. If they are equal, display the message "The numbers are equal".
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.
#The program takes a temperature and displays: "Cold" (below 10), "Warm" (10–24), "Hot" (25 and above).
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.
#The user enters a number. Check whether it 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.
#The user enters a password. If the password length is less than 6 characters — display "Password is too short", otherwise — "Password accepted".
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.
#The user enters a grade from 0 to 100. Display: A (90–100), B (80–89), C (70–79), D (60–69), F (less than 60).
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.
#The user enters two numbers. Check whether the first number is divisible by the second.
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.
#The user enters a number. If it is greater than 100 or less than 0 — display "Only from 0 to 100", otherwise round it to 2 decimal places.
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.
#The user enters a string. If the string length is greater than 10 characters — display "Long string", otherwise — "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.
#The user enters a and b. Calculate what percentage a is of b. Percentage formula: (a/b)*100.
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.
#The user enters a number, for example 5.2564494 and enters how many decimal places it should be rounded to. Perform the calculations.
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.
#The user enters a number as a string. Check whether the string contains a dot, and in any case convert the value to the number type using 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.
#You need to divide a by b. If b equals 0, display a message that division by 0 is not allowed. Otherwise, calculate the result!
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.
#The user enters a First Name and then separately a Last Name. Let's try to make it so that if everything is entered correctly, we combine the First Name and Last Name into one string (as one new object) and print it to the terminal. But if the user makes a mistake and enters both the First Name and Last Name immediately in the "First Name" field of the form, then display a message saying: "Fill out the form carefully!! That was the last blank form hahaha)))" and terminate the program. Hints: 1) There will be a space in the string if everything is entered together. 2) First check the incorrect case, it will be simpler and more correct.
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);
}