How to find the maximum number in javascript?
You can use Math.max
var FirstNumber = Number(prompt("Enter the First number"));
var SecondNumber = Number(prompt("Enter the Second number"));
var ThirdNumber= Number(prompt("Enter the third number"));
var ForthNumber = Number(prompt("Enter the forth number"));
var max = Math.max(FirstNumber, SecondNumber, ThirdNumber, ForthNumber);
document.writeln("<p>" + max + " is the largest number </p>");
Note that this does not handle bad user input (like entering letters, scientific notation, etc). You should validate if you wanna avoid NaN (not a number) issues.
A solid way to format the user input is to use a formatter NumberFormat
Aucun commentaire