10 JavaScript If else exercises with solution

1. Check if a number is odd or even in JavaScript

Function `isEvenOrOdd()` checks if input number is even or odd by using “%” operator in JavaScript.

  1. Print “Number is even” if the number is divisible by 2.
  2. Else print “Number is odd” if the number returns a remainder when divided by 2.
function isEvenorOdd(num) {

  if(num % 2 == 0){
    console.log(`${num} is a Even number`)
  }
  else{
    console.log(`${num} is a Odd number`)
  }
}

isEvenorOdd(10) //"10 is a Even number"
isEvenorOdd(19) //"19 is a Odd number"

2. Check if input variable is a number or not

Function `isNumber()` checks if input variable is a number by using isNaN() in-built JavaScript function. Read more about isNan() from w3schools.com/jsref/jsref_isnan.asp.

  1. Print “Variable is not a number” if isNaN() returns true.
  2. Else print “Variable is a valid number” if isNaN() returns false.
function isValidNumber(num) {

  if(isNaN(num)){
      console.log(`${num} is not a number`)
  }
  else{
    console.log(`${num} is a valid number`)
  }
}

isValidNumber(11) //"11 is a valid number"
isValidNumber("19") //"19 is a valid number"
isValidNumber("xyz") //"xyz is not a number"
isValidNumber("17.5") //"17.5 is a valid number"
isValidNumber("21F") //"21F is not a number" 

3. Find the largest of two number

Function `findLargest()` finds the largest between two number by using “>” and “=” operator in JavaScript.

  1. Print num1 is the largest if num1>num2.
  2. Print num2 is the largest if num1<num2.
  3. Else print num1 and num2 are equal when num1==num2.
function findLargest(num1, num2) {

  if(num1 > num2){
    console.log(`${num1} is the largest number`)
  }
  else if (num2 > num1){
    console.log(`${num2} the largest number`)
  }
  else{
    console.log(`${num1} is equal to ${num2}`)
  }
}

findLargest(21,45) //"45 the largest number"
findLargest(34,18) //"34 is the largest number"
findLargest(41,41) //"41 is equal to 41"

4. Find the largest of three number

Function `findLargest()` finds the largest of three number by using “>” and “&&” operator in JavaScript.

  1. Print num1 is the largest if num1>num2 and num1>num3.
  2. Print num2 is the largest if num2<num3.
  3. Else print num3.
function findLargest(num1, num2, num3) {

  if(num1 > num2 && num1 > num3){
    console.log(`${num1} is the largest number`)
  }
  else if (num2 > num3){
    console.log(`${num2} is the largest number`)
  }
  else{
    console.log(`${num3} is the largest number`)
  }
}

findLargest(21,45,13) //"45 is the largest number"
findLargest(34,18,52) //"52 is the largest number"
findLargest(64,11,11) //"64 is the largest number"

5. Check if a triangle is equilateral, scalene, or isosceles

Function `findTriangleType()` finds the type of the triangle for given side values by using “==” and “&&” operator in JavaScript.

  1. Print “Equilateral triangle.” if values for all side1, side2 and side3 are equal.
  2. Print “Isosceles triangle.” if values for side1 is equal to side2 or side2 is equal to side3
  3. Else “Scalene triangle.” since values of all sides are unequal.
function findTriangleType(side1, side2, side3) {

  if((side1 == side2) && (side1 == side3)){
    console.log(`Equilateral triangle.`)
  }
  else if ((side1 == side2) || (side2 == side3)){
    console.log(`Isosceles triangle.`)
  }
  else{
    console.log(`Scalene triangle.`)
  }
}

findTriangleType(12,12,12) //"Equilateral triangle."
findTriangleType(20,20,31) //"Isosceles triangle."
findTriangleType(5,4,3) //"Scalene triangle."

6. Find the a number is present in given range

Function `checkInRange()` finds if the given number is within the provided start and end range using >=, <= and && operators in JavaScript.

  1. Print “Between the range” if num is between start and end values
  2. Else Print “Outside the range” since num is outside start and end values.
function checkInRange(num, start, end) {

  if(num >= start && num <= end){
    console.log(`${num} is between the range ${start} and ${end}`)
  }
  else{
    console.log(`${num} is outside the range ${start} and ${end}`)    
  }
}

checkInRange(15,11,30) //"15 is between the range 11 and 30"
checkInRange(20,34,51) //"20 is outside the range 34 and 51"

7. Perform arithmetic operations on two numbers

Function `evalNumbers()` prints the result after evaluating arithmetic operations between two numbers of addition, multiplication, division, and modulus in JavaScript.

  1. Print result of num1+num2 if operation is “add”
  2. Print result of num1-num2 if operation is “subtract”
  3. Print result of num1*num2 if operation is “multiply”
  4. Print result of num1/num2 if operation is “divide”
  5. Print result of num1%num2 if operation is “modulus”
  6. Else print “Invalid operation”
function evalNumbers(num1, num2, op) {

  if(op == "add"){
    console.log(`Sum of ${num1} and ${num2} is ${num1+num2}`)
  }
  else if(op == "subtract"){
    console.log(`Difference of ${num1} and ${num2} is ${num1-num2}`)
  }
  else if(op == "multiply"){
    console.log(`Product of ${num1} and ${num2} is ${num1*num2}`)
  }
  else if(op == "divide"){
    console.log(`Division of ${num1} and ${num2} is ${num1/num2}`)
  }
  else if(op == "modulus"){
    console.log(`Modulus of ${num1} and ${num2} is ${num1%num2}`)
  }
  else{
     console.log(`${op} is an invalid operation`)    
  }
}

evalNumbers(15,10,"add") //"Sum of 15 and 10 is 25"
evalNumbers(20,8,"subtract") //"Difference of 20 and 8 is 12"
evalNumbers(12,4,"multiply") //"Product of 12 and 4 is 48"
evalNumbers(28,7,"divide") //"Division of 28 and 7 is 4"
evalNumbers(22,3,"modulus") //"Modulus of 22 and 3 is 1"
evalNumbers(31,3,"square") //"square is an invalid operation"

8. Find check if a year is leap year or not

Function `checkLeapYear()` find if the given year is a leap year or not by using %, !=, && and || operators in JavaScript.

  1. If year is divisble by 4 and not divisble by 100 then print “leap year”.
  2. Or if year is divisible by 400 then print “leap year”.
  3. Else print “not a leap year”.
function checkLeapYear(year) {

  if(((year%4 == 0)&&(year%100 != 0))||(year%400 == 0)){
    console.log(`Year ${year} is a leap year`);
  }
  else{
    console.log(`Year ${year} is not a leap year`);
    }
 }

checkLeapYear(2012) //"Year 2012 is a leap year"
checkLeapYear(1900) //"Year 1900 is not a leap year"
checkLeapYear(2000) //"Year 2000 is a leap year"
checkLeapYear(2011) //"Year 2011 is not a leap year"

9. Find the grade for input marks

Function `findGrade()` to find the grade of the student based on the input marks.

  1. Print “S grade” if marks is between 90 and 100.
  2. Print “A grade” if marks is between 80 and 90.
  3. Print “B grade” if marks is between 70 and 80.
  4. Print “C grade” if marks is between 60 and 70.
  5. Print “D grade” if marks is between 50 and 60.
  6. Print “E grade” if marks is between 40 and 50.
  7. Print “Student has failed” if marks is between 0 and 40.
  8. Else print “Invalid marks”.
function findGrade(name, marks) {

  if(marks >= 90 && marks <= 100){
    console.log(`${name} you have received S grade`)
  }
  else if(marks >= 80 && marks < 90){
    console.log(`${name} you have received A grade`)
  }
  else if(marks >= 70 && marks < 80){
    console.log(`${name} you have received B grade`)
  }
  else if(marks >= 60 && marks < 70){
    console.log(`${name} you have received C grade`)
  }
  else if(marks >= 50 && marks < 60){
    console.log(`${name} you have received D grade`)
  }
  else if(marks >= 40 && marks < 50){
    console.log(`${name} you have received E grade`)
  }
  else if(marks >= 0 && marks <40){
    console.log(`${name} you have Failed`)
  }
  else{
    console.log(`Invalid marks of ${marks}`)
  }
}

findGrade("John Doe", 91) //"John Doe you have received S grade"
findGrade("John Doe", 85) //"John Doe you have received A grade"
findGrade("John Doe", 73) //"John Doe you have received B grade"
findGrade("John Doe", 53) //"John Doe you have received D grade"
findGrade("John Doe", 20) //"John Doe you have Failed"
findGrade("John Doe", 120) //"Invalid marks of 120"

10. Find number of days in a given month

Function `findDaysInMonth()` finds the number of days in a given month of a year.

  1. If month is outside the range of 1 and 12 print “Invalid month”.
  2. If month is equal to 2 ie, February print “29 days” if leap year else print “28 days” .
  3. Else if month is equal to 4, 6, 9 or 11 print “30 days”.
  4. Else print “31 days”.
function isLeapYear(year){

  return (((year%4 == 0)&&(year%100 != 0))||(year%400 == 0));
}

function findDaysInMonth(month, year) {

  if(month < 1 || month > 12){
    console.log(`Invalid Month of value ${month}`)
    return;
  }

  if(month ==2){
    if(isLeapYear(year)){
       console.log(`The Month has 29 days`)    
    }
    else{
       console.log(`The Month has 28 days`)    
    }
  }
  else if(month == 4 || month == 6 || month == 9 || month== 11){
     console.log(`The Month has 30 days`)    
  }
  else{
     console.log(`The Month has 31 days`)
  }
}

findDaysInMonth(2, 2012) //"The Month has 29 days"
findDaysInMonth(2, 2013) //"The Month has 28 days"
findDaysInMonth(4, 2012) //"The Month has 30 days'
findDaysInMonth(10, 2013) //"The Month has 31 days"

Video for Reference