JavaScript · Syntax · Beginner

Conditions

15 tasks

Practical exercises on conditional statements in JavaScript: comparing numbers, checking ranges, working with strings, passwords, percentages, and basic if-else logic.

How JavaScript makes decisions

#
Conditional statements in JavaScript work the same way as in most languages: look at a value, choose a path. ```javascript if (condition) { // runs when condition is true } else if (anotherCondition) { // runs when the first was false, but this one is true } else { // runs when nothing above matched } ``` Unlike Python, JavaScript uses curly braces to mark blocks and parentheses around the condition — both are required. Indentation is convention, not syntax. JavaScript checks conditions top to bottom and stops at the first that is true. Only one branch runs. Order matters: put the most specific checks first. else if and else are both optional. A plain if with no else is perfectly valid when you only need to act in one specific case.

Comparing values and combining conditions

#
Most conditions are comparisons. JavaScript has two equality operators, and this is one of the first things to get right: ```javascript a === b // true when a and b are equal AND the same type a !== b // true when they differ in value or type a > b a < b a >= b a <= b ``` Use === and !== for almost everything. The loose equality operator == does type coercion: 0 == false is true, "1" == 1 is true. These surprises are a common source of bugs. Stick with strict equality. Combining conditions with logical operators: ```javascript if (age >= 13 && age <= 17) { console.log("Teenager"); } if (score < 0 || score > 100) { console.log("Score out of range"); } if (!isAdmin) { console.log("Access denied"); } ``` && requires both sides to be true. || requires at least one. ! negates. JavaScript does not support chained comparisons like Python's 13 <= age <= 17. Write the two comparisons explicitly with &&.

Common patterns with conditions

#
In these exercises the function receives values and makes a decision based on them. Numbers arrive already as numbers (unlike Python's input()), so no conversion is usually needed. ```javascript function checkSign(number) { if (number > 0) { return "Positive"; } else if (number < 0) { return "Negative"; } else { return "Zero"; } } ``` Checking divisibility with the modulo operator: ```javascript function isEven(n) { if (n % 2 === 0) { return "Even"; } else { return "Odd"; } } ``` % returns the remainder after integer division. n % 2 === 0 is true for every even number. The same idea scales: n % 3 === 0 checks divisibility by 3. Guarding against division by zero: ```javascript function divide(a, b) { if (b === 0) { return "Cannot divide by zero"; } return a / b; } ``` Always check the divisor before dividing. In JavaScript, dividing by zero does not throw an error — it returns Infinity or NaN. That is rarely what you want, so the guard is still necessary.
01

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);
02

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");
}
03

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);
04

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");
}
05

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");
}
06

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");
}
07

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");
}
08

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");
}
09

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");
}
10

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);
11

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) + "%");
}
12

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)));
}
13

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);
14

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);
}
15

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);
}