Revision and Problem Solving
Read This on Github
➡️ Question 1
Use logical operators to find whether
the age of a person lies between
13 and 19 in JavaScript.
let age = prompt("Please enter your age:");
age = parseInt(age); // convert the age to an integer
if (age >= 13 && age <= 19) {
console.log("Your age is between 13 and 19");
} else {
console.log("Your age is not between 13 and 19");
}
⚡ Playground
➡️ Question 2
Demonstrates the use of switch
case statements in javascript.
let age = 15;
switch (true) {
case age >= 13 && age <= 19:
console.log("Your age is between 13 and 19");
break;
default:
console.log("Your age is not between 13 and 19");
}
⚡ Playground
➡️ Question 3
Write a javascript program to find
whether a number is divisible by
two and three or not.
let number = 12;
if (number % 2 == 0 && number % 3 == 0) {
console.log(`${number} is divisible by 2 and 3`);
} else {
console.log(`${number} is not divisible by 2 and 3`);
}
This code will check if the value of the number variable is divisible by 2 and 3 by using the modulo operator to calculate the remainder of number divided by 2 and 3. If both remainders are equal to 0, it will print a message saying that the number is divisible by 2 and 3. If either remainder is not equal to 0, it will print a message saying that the number is not divisible by 2 and 3.
For example, if the value of number is 12, the code will output: "12 is divisible by 2 and 3"
You can also use the logical OR operator (||) to check if the number is divisible by 2 or 3:
if (number % 2 == 0 || number % 3 == 0) {
console.log(`${number} is divisible by 2 or 3`);
} else {
console.log(`${number} is not divisible by 2 or 3`);
}
This will check if the value of the number variable is divisible by 2 or 3 by using the modulo operator to calculate the remainder of number divided by 2 and 3. If either remainder is equal to 0, it will print a message saying that the number is divisible by 2 or 3. If both remainders are not equal to 0, it will print a message saying that the number is not divisible by 2 or 3.
⚡ Playground
$
is not a special character or symbol. It is simply being used as a placeholder for the value of the number variable inside a string.
The ${}
syntax is used to embed a JavaScript expression inside a string. In this case, the expression number is being replaced with the value of the number variable.
For example, the following code:
let number = 12;
console.log(`${number} is a number`);// 12 is a number
The console.log function is used to print a message to the console. The message being printed is a string that contains the value of the number variable inside a pair of $ curly braces. This is equivalent to writing:
console.log(number + " is a number");
But using template literals allows you to embed expressions inside strings in a more concise and readable way.