0

I do not know what is wrong with my code but even if the first number is the largest, it does not display it but displays the second largest number

I did as one of suggested but my problem is that for example if I put the numbers as (100,58,12,0) the first number 100 will not display but 58 will which I find confusing

thanks for everyone who helped mr, I found the solution to my problem

    var FirstNumber;
FirstNumber = window.prompt("Enter the First number");

var SecondNumber;
SecondNumber = window.prompt("Enter the Second number");

var ThirdNumber;
ThirdNumber = window.prompt("Enter the third number");

var ForthNumber;
ForthNumber = window.prompt("Enter the forth number");

var max = Math.max(FirstNumber, SecondNumber, ThirdNumber, ForthNumber);

document.writeln("<p>" + max + " is the largest number </p>");
4
  • 2
    Is there any reason you're doing it like this instead of using Math.max() ? Commented Mar 14, 2020 at 15:16
  • max = Math.max(FirstNumber, SecondNumber, ThirdNumber, FourtNumber) Commented Mar 14, 2020 at 15:17
  • 1
    I cannot reproduce your problem. It always returns the lexicographically largest number for me. Commented Mar 14, 2020 at 15:17
  • You should make clear what the original problem was, and whether the code you've posted is the problem or the solution. If it's not the solution, maybe post the code which worked so other people with the same problem will be able to solve it! You can actually answer your own question too, btw. Commented Mar 14, 2020 at 16:59

2 Answers 2

2

TLDR: 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>");

Math max will work fine as long as the user input is a valid JS number

Note that this does not handle bad user input (like entering commas, letters, scientific notation, etc). You should validate if you wanna avoid NaN (not a number) issues.

To validate the user input you can create your own Regular Expression or use a more advanced library like Numeral.js

A solid way to format the user output is to use a intl formatter NumberFormat

If you plan to work with integers larger than Number.MAX_SAFE_INTEGER (253-1 = 9007199254740991) you need to use BigInt

Sign up to request clarification or add additional context in comments.

Comments

1

This approach does not use Math.max.

You could take numbers from prompt with an unary plus +.

Then take a linear approach and use the first value as max value and check the second value and update if max smaller.

After checking each following number, you have the maximum value.

var firstNumber = +prompt("Enter the First number"),
    secondNumber = +prompt("Enter the Second number"),
    thirdNumber = +prompt("Enter the third number"),
    forthNumber = +prompt("Enter the forth number"),
    max = firstNumber;

if (max < secondNumber) max = secondNumber;
if (max < thirdNumber) max = thirdNumber;
if (max < forthNumber) max = forthNumber;

console.log(max);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.