Practice set 2 in JavaScript

 //Chapter 2 : Practice Set

//Question 1 : Use logical operators to find whether the age of a person lies between 10 and 20 ?
let age = prompt("What is your age ?");
age = Number.parseInt(age);
if (age > 10 && age < 20) {
    console.log("Your age lies between 10 and 20");
}
else {
    console.log("Your age does not lies between 10 and 20");
}


//Question 2 : Demonstrate the use of switch case statements in JavaScript
let ageTwo = prompt("What is your age ?");
switch (ageTwo) {
    case "12":
        console.log("Your age is 12");
        break;
    case "13":
        console.log("Your age is 13");
        break;
    case "14":
        console.log("Your age is 14");
        break;
    case "15":
        console.log("Your age is 15");
        break;
    case "16":
        console.log("Your age is 16");
        break;
    default:
        console.log("Your age is not special");
        break;
}


//Question 3 : Write a JavaScript program to find whether a number is divisible by 2 and 3
let num = prompt("What is your number ?");
num = Number.parseInt(num);
if (num % 2 == 0 && num % 3 == 0) {
    console.log("Number is divisible by 2 and 3");
}
else {
    console.log("Number is not divisible by 2 and 3");
}


//Question 4 : Write a JavaScript program to find whether a number is divisible by either 2 or 3
let numTwo = prompt("What is your number ?");
numTwo = Number.parseInt(numTwo);
if (numTwo % 2 == 0 || numTwo % 2 == 0) {
    console.log("Number is divisible by 2 or 3");
}
else {
    console.log("Number is not divisible by 2 or 3");
}


//Question 5 : Print "You can drive" OR "You can not drive" based on age being greater than 18 using ternary operator
let ageThree = prompt("What is your age ?");
ageThree = Number.parseInt(ageThree);
ageThree > 18 ? console.log("You can drive") : console.log("You can not drive");

Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript