JavaScript: Find the largest of five numbers
JavaScript Conditional Statement and loops: Exercise-4 with Solution
Largest of Five Numbers
Write a JavaScript conditional statement to find the largest of five numbers. Display an alert box to show the results.
Sample numbers : -5, -2, -6, 0, -1
Output : 0
Visual Presentation:
JavaScript: Find the largest of five numbersSample Solution:
JavaScript Code:
// Assigning values to variables
var a = -5;
var b = -2;
var c = -6;
var d = 0;
var f = -1;
// Checking the conditions to determine the maximum value
if (a> b && a> c && a> d && a> f) {
console.log(a);
} else if (b> a && b> c && b> d && b> f) {
console.log(b);
} else if (c> a && c> b && c> d && c> f) {
console.log(c);
} else if (d> a && d> c && d> b && d> f) {
console.log(d);
} else {
console.log(f);
}
Output:
0
Flowchart:
Flowchart: JavaScript:- Find the largest of five numbersLive Demo:
See the Pen javascript-conditional-statements-and-loops-exercise-4 by w3resource (@w3resource) on CodePen.
For more Practice: Solve these Related Problems:
- Write a JavaScript function that finds the largest number among five integers using recursion.
- Write a JavaScript function that iteratively compares five numbers without using Math.max to determine the largest value.
- Write a JavaScript function that splits the array of five numbers and uses a divide and conquer approach to identify the maximum.
- Write a JavaScript function that finds the largest of five numbers by simulating a tournament-style elimination.
Go to:
PREV : Sort Three Numbers.
NEXT : Odd or Even Loop.
Improve this sample solution and post your code through Disqus.