Find the Greatest of the Three Numbers in Javascript
We will discuss three methods to solve this problem using javascript
Introduction
Hi everyone today we find the Greatest of the Three Numbers in Javascript. let's discuss how to solve this problem statement. if you want to check out yesterday's problem click here.
The goal is to develop a Javascript code to get the largest of the three numbers given three integer inputs, num1, num2, and num3. We'll explore a Javascript program to find the greatest of three integers in this post. To discover the same, we will also utilize the ternary operator and if else conditions. The following are various solutions to the difficulty indicated above:
Method 1: Using if-else Statements 2
Method 2: Using if-else Statements 2
Method 3: Using Ternary Operator
Method 1: Using if-else Statements 2
Algorithm
step 1: Take three number inputs from the user
step 2: compare (num1>=num2 && num1>=num3) if true num1 is greatest
step 3: compare (num2>=num1 && num2>=num3) if true num2 is greatest
step 4: compare (num3>=num1 && num3>=num2) if true num3 is greatest
Javascript Code
let num1 = 33, num2 = 22, num3=11
//checking if num1 is greatest
if(num1>=num2 && num1>=num3)
console.log(num1+" is the greatest");
// checking if num2 is greatest
if(num2>=num1 && num2>=num3)
console.log(num2+" is the greatest");
// checking if num2 is greatest
if(num3>=num1 && num3>=num2)
console.log(num3+" is the greatest");
Output
33 is greatest
Method 2: Using if-else Statements 2
Algorithm
step 1: Take three number inputs from the user
step 2: compare (num1>=num2 && num1>=num3) if true num1 is greatest
step 3: compare (num2>=num1 && num2>=num3) if true num2 is greatest
step 4: if all the above conditions are false then num3 has to be the greatest.
Javascript Code
let num1 = 10, num2 = 20, num3 =30;
// Comparing num1 with other numbers
if((num1>= num2) && (num1>=num3)){
console.log(num1+ ' is the greatest');
//checking if num2 is greatest
}else if(num2>=num1 && num2>=num3){
console.log(num2+" is the greatest");
}else{
//num3 has to be greatest then if not above
console.log(num3+" is the greatest");
}
Output
30 is the greatest
Method 3: Using Ternary Operator
Algorithm
step 1: Take three number inputs from the user
step 2: Take temp and result for storing the output
step 3: temp = num1>num2 ? num1 : num2
step 4: result = temp > num3 ? temp: num3
step 5: print the result.
Javascript Code
let num1 = 5, num2 = 15, num3 = 25;
let temp ,result;
// find the largest between num1 and num2 & store in temp
temp = num1>num2 ? num1 : num2;
// find the largest between and num3 & finally printing it.
result = temp > num3 ? temp : num3;
console.log(result+" is the greatest.")
Output
25 is greatest
Conclusion
So this article we solved the problem statement using javascript language. I hope you found this article helpful for your web development journey.
If you fill this article informative please like this article, if you have any suggestions please comment on this article, share it on social media and follow me on
Hashnode - Gaurav Patil
Twitter - @GauravYPatil