173,995 views
27 votes
27 votes
Write JavaScript code to display greatest number among any three numbers​

User Stevenrcfox
by
2.9k points

1 Answer

20 votes
20 votes
Here is one possible solution using JavaScript:


function findGreatest(a, b, c) {
let greatest = a;
if (b > greatest) {
greatest = b;
}
if (c > greatest) {
greatest = c;
}
return greatest;
}
You can use this function by passing in any three numbers, like this:


let num1 = 5;
let num2 = 10;
let num3 = 7;
let greatest = findGreatest(num1, num2, num3);
console.log(greatest); // This will print 10 to the console
This code will find the greatest number among the three numbers a, b, and c by first setting the initial value of greatest to the value of a. It then checks if b is greater than greatest and updates the value of greatest if that is the case. Finally, it checks if c is greater than greatest and updates the value again if necessary. The function then returns the value of greatest, which is the largest of the three numbers
User Bill Posters
by
2.9k points