Before reading this code you should have an understanding of the if else statement in javascript And a little bit about the logical AND operator.
Logical AND(&&) returns true only if all the equation is true. For example
true && false = false
true && true && false = false
true && true = true
Steps of the program:
Let’s declare and initialize the five variables. The five variables are a,b,c,d,e.
We are going to use an if-else-if statement.
Line number 4: First of all we will compare variables a with all the variables. If a is the largest good but if don’t then we will exclude a in the next step.
Line number 8: We will compare b with all the variables except a. If b is the largest good but if don’t then we will exclude b in the next step.
Line number 12: We will compare variable c with d and e. If c is the largest then good but if don’t we will exclude c in next step.
Line number 16: We will compare variable d with e. If d is largest then print d else print e.
let a = 5,b = 1,c = 100,d = 22,e = 8;
console.log("The largest of five numbers are:");
if(a>b && a>c && a>d && a>e)
{
console.log(a);
}
else if(b>c && b>d && b>e)
{
console.log(b);
}
else if(c>d && c>e)
{
console.log(c);
}
else if(d>e)
{
console.log(d);
}
else
{
console.log(e);
}
The output of the code is
The largest of five numbers are:
22