Write a program in JavaScript to find the largest of three numbers.
Answers
Answer:
For largest three number / program to find the largest among three numbers
// take input from the user
const num1 = parseFloat(prompt("Enter first number: "));
const num2 = parseFloat(prompt("Enter second number: "));
const num3 = parseFloat(prompt("Enter third number: "));
let largest;
// check the condition
if(num1 >= num2 && num1 >= num3) {
largest = num1; } else if (num2 >= num1 && num2 >= num3) {
largest = num2; } else {
largest = num3; }
// display the result
console.log("The largest number is " + largest);
Output
Enter first number: -7
Enter second number: -5
Enter third number: -1
The largest number is -1
Explanation:
How to find largest number in JavaScript?
reduce() can be used to find the maximum element in a numeric array, by comparing each value:
var arr = [1,2,3]; var max = arr. reduce(function(a, b) { return Math. max(a, b); }); ...
function getMaxOfArray(numArray) { return Math. max. apply(null, numArray); } ...
var arr = [1, 2, 3]; var max = Math. max(... arr);