JavaScript · Syntax · Beginner
Conditions 2
Additional practice with conditionals in JavaScript: string checks, text length, character checks, slicing via string methods, logical operators &&/||, and simple input scenarios.
Quick topic start and explanations before exercises (exercises below):
Patterns from the exercises
#Exercises:
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");
}
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");
}
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);
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");
}
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");
}
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");
}
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");
}
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");
}
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");
}
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");
}
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!");
}