JavaScript · Syntax · Beginner

Conditions 2

11 tasks

Additional practice with conditionals in JavaScript: string checks, text length, character checks, slicing via string methods, logical operators &&/||, and simple input scenarios.

String operations for conditions

#
String exercises at this level require knowing which string operations to use for comparisons and checks. JavaScript strings have a clean set of methods for this. Character access uses bracket notation. JavaScript does not have negative indices, but the at() method does: ```javascript const text = "JavaScript"; console.log(text[0]); // J — first character console.log(text.at(-1)); // t — last character console.log(text.at(-2)); // p — second to last ``` Slicing uses slice(start, end). Unlike Python, there is no step argument, but the syntax is otherwise identical: ```javascript const text = "Hello world"; console.log(text.slice(0, 4)); // Hell — first 4 characters console.log(text.slice(2, 8)); // llo wo ``` If you omit the second argument, slice goes to the end of the string. For substring and prefix/suffix checks: ```javascript text.includes("@") // true when "@" appears anywhere text.startsWith("Py") // true when text begins with "Py" text.endsWith("on") // true when text ends with "on" ``` All three are case-sensitive. "python".startsWith("Py") is false. String length is a property, not a method: ```javascript text.length // number of characters — no parentheses ``` A common mistake is writing text.length() — that will throw a TypeError.

Patterns from the exercises

#
Before accessing characters at a specific index, check that the string is long enough: ```javascript const text = prompt("Enter a string: "); if (text.length > 10) { console.log(text[0], text.at(-1)); } else { console.log("Too short"); } ``` For case-insensitive comparisons, convert to lowercase first. toLowerCase() returns a new string: ```javascript const text = prompt("Enter a phrase: "); if (text.toLowerCase() === "hello") { console.log("Hello to you too!"); } else if (text.toLowerCase() === "goodbye") { console.log("Goodbye!"); } else { console.log("I don't understand"); } ``` Combining a length check with a content check — && short-circuits from left to right, so if the length check fails JavaScript skips the rest: ```javascript const text = prompt("Enter a message: "); if (text.length > 6 && text.at(-1) === "!") { console.log("Emotional message"); } else { console.log("Normal message"); } ``` Checking a range of lengths: ```javascript if (text.length >= 5 && text.length <= 10) { console.log("Suitable length"); } ``` JavaScript does not have Python's chained comparison — you always need the two separate conditions joined with &&.
01

First and last character.

#

The user enters a string. If the string length is greater than 10 characters — display the first and last character, otherwise — display "The string is too short".

let text = "JavaScript course";
Solution
let text = "JavaScript course";
if (text.length > 10) {
    console.log(text[0], text[text.length - 1]);
} else {
    console.log("The string is too short");
}

// or you can do it this way: store the characters in variables
let text = "JavaScript course";

if (text.length > 10) {
    let first = text[0];
    let last = text[text.length - 1];
    console.log(first, last);
} else {
    console.log("The string is too short");
}
02

String starts with A.

#

The user enters a string. If the string starts with the letter "A" or "a" — display "Starts with A", otherwise — display "Starts with another letter". You may use string methods.

let text = "Alice";
Solution
let text = "Alice";
if ((text.startsWith("A")) || (text.startsWith("a"))) {
    console.log("Starts with A");
} else {
    console.log("Starts with another letter");
}
03

First four characters.

#

The user enters a string. If the string length is greater than 8 characters — display the first 4 characters, otherwise — display the whole string.

let text = "JavaScript";
Solution
let text = "JavaScript";
if (text.length > 8) {
    console.log(text.slice(0, 4));
} else {
    console.log(text);
}

// or you can do it this way: prepare the fragment first
let text = "JavaScript";
let part;

if (text.length > 8) {
    part = text.slice(0, 4);
} else {
    part = text;
}

console.log(part);
04

Emotional message.

#

The user enters a string. If the string length is greater than 6 characters and the last character is "!" — display "Emotional message", otherwise — "Regular message".

let text = "JavaScript!";
Solution
let text = "JavaScript!";
if ((text.length > 6) && (text[text.length - 1] === "!")) {
    console.log("Emotional message");
} else {
    console.log("Regular message");
}

// or you can do it this way with the endsWith method
let text = "Hello!";

if (text.length > 6 && text.endsWith("!")) {
    console.log("Emotional message");
} else {
    console.log("Regular message");
}
05

Searching for special characters.

#

The user enters a string. If the string contains the symbol "@" or the symbol "#" — display "Special character found", otherwise — "No special characters".

let text = "[email protected]";
Solution
let text = "[email protected]";
if ((text.includes("@")) || (text.includes("#"))) {
    console.log("Special character found");
} else {
    console.log("No special characters");
}

// or you can do it this way: check characters one by one
let text = "hello@site";

if (text.includes("@")) {
    console.log("Special character found");
} else if (text.includes("#")) {
    console.log("Special character found");
} else {
    console.log("No special characters");
}
06

Greeting by first and last name.

#

The user enters a first name and last name. If both the first name and the last name are not empty — display a greeting in the format: "Hello, First Name Last Name!" (use a template literal), otherwise — "Enter valid data".

let name = "Alex";
let surname = "Smith";
Solution
let name = "Alex";
let surname = "Smith";
if ((name) && (surname)) {
    console.log(`Hello, ${name} ${surname}!`);
} else {
    console.log("Enter valid data");
}

// or without a template literal, using string concatenation
let name = "Alex";
let surname = "Smith";

if (name && surname) {
    console.log("Hello, " + name + " " + surname + "!");
} else {
    console.log("Enter valid data");
}
07

Fragment from the 3rd to the 8th character.

#

The user enters a string. If the string length is greater than 10 characters — display a string fragment using slice from the 3rd character (inclusive) to the 8th character (inclusive), otherwise — display "Not enough characters".

let text = "JavaScript course";
Solution
let text = "JavaScript course";
if (text.length > 10) {
    console.log(text.slice(2, 8));
} else {
    console.log("Not enough characters");
}
08

First and last in a message.

#

The user enters a string. If the string is not empty — display the message: "First character: X, last character: Y" (use a template literal), otherwise — "Empty string".

let text = "JavaScript";
Solution
let text = "JavaScript";
if (text) {
    console.log(`First character: ${text[0]}, last character: ${text[text.length - 1]}`);
} else {
    console.log("Empty string");
}

// or you can do this: save the first and last character beforehand
let text = "JavaScript";

if (text) {
    let first = text[0];
    let last = text[text.length - 1];
    console.log(`First character: ${first}, last character: ${last}`);
} else {
    console.log("Empty string");
}
09

Suitable string length.

#

The user enters a string. If the string length is from 5 to 10 characters inclusive — display "Suitable length", otherwise — "Unsuitable length".

let text = "JavaScript";
Solution
let text = "JavaScript";
if (5 <= text.length && text.length <= 10) {
    console.log("Suitable length");
} else {
    console.log("Unsuitable length");
}

// second option: the same thing with a separate length variable
let text = "course";
let length = text.length;

if (length >= 5 && length <= 10) {
    console.log("Suitable length");
} else {
    console.log("Unsuitable length");
}
10

Looks like JavaScript.

#

The user enters a string. If the string starts with "Java" or ends with "Script" — display "Looks like JavaScript", otherwise — "Does not look like JavaScript".

let text = "JavaScript";
Solution
let text = "JavaScript";
if ((text.startsWith("Java")) || (text.endsWith("Script"))) {
    console.log("Looks like JavaScript");
} else {
    console.log("Does not look like JavaScript");
}
11

Hello or goodbye.

#

"Hello - Goodbye program" If the user enters: Hello, hello, hElLo or HELLO - reply with "Hello to you too!" If the user enters: Bye, bYe, etc. - reply with "Adios!".

let text = "Привет";
Solution
let text = "Hello";
if (text.toLowerCase() === "hello") {
    console.log("Hello to you too!");
} else if (text.toLowerCase() === "bye") {
    console.log("Adios!");
} else {
    console.log("Nah, I don't talk to strangers! Adios!");
}