JavaScript if else exercises | Solution Code

  • by

We will cover most frequently questions used by JavaScript (web app) developers. These codes are mostly used JavaScript if else condition statements. Just do this if-else exercises program practice for a better hand on JavaScript code.

The conditional statement checks certain conditions and then based on the truth value of the condition, executes a block of code.

if (condition) 
{     
   statement 1... 
} 
else 
{     
   statement 2... 
}

JavaScript if else exercises

Simple example code. Conditional statements in JavaScript uses if-else or switch statements that change the direction of the program in run-time.

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

Use the “%” operator in the if block to get the type of number.

<script>
    function isEvenorOdd(num) {

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

    isEvenorOdd(10)
    isEvenorOdd(99)

</script>

Output:

JavaScript if else exercises

2. Find the largest of two number

Print num1 is the largest if num1>num2 else Print num2 is the largest if num1<num2. On none true condition print num1 and num2 are equal.

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) 

3. Perform arithmetic operations on two numbers

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(1,2,"add") 
evalNumbers(2,8,"subtract") 
evalNumbers(2,4,"multiply") 
evalNumbers(28,7,"divide") 
evalNumbers(22,3,"modulus") 
evalNumbers(31,3,"square")

5. Find the grade for input 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", 91) 
findGrade("Mark", 85) 
findGrade("Jen", 73) 
findGrade("Steve", 53)
findGrade("Tim", 20) 
findGrade("Doe", 120)

6. sort three numbers

var x= 0;
var y=-1;
var z= 4;
if (x>y && x>z)
{
        if (y>z)
        {
            console.log(x + ", " + y + ", " +z);
        }
        else
        {
            console.log(x + ", " + z + ", " +y);
        }
}
else if (y>x && y >z)
{
        if (x>z)
        {
             console.log(y + ", " + x + ", " +z);
        }
        else
        {
             console.log(y + ", " + z + ", " +x);
        }
}
else if (z>x && z>y)
{
        if (x>y)
        {
            console.log(z + ", " + x + ", " +y);
        }
        else
        {
            console.log(z + ", " + y + ", " +x);
        }
}        

Do comment if you have any doubts or suggestions on this JS code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.