-3

The user is prompted to enter three numbers of their choice. When a 9 digit number is entered and is compared with a smaller number it says the smaller number is larger.

function myFunction(){
  var num1 = prompt("Enter your first number");
  var num2 = prompt("Enter your second number");
  var num3 = prompt("Enter your third number");

  if (num1 > num2 && num1 > num3){
 document.getElementById("result").innerHTML = "The biggest number was " +num1+ "!!";
  } else if (num2 > num1 && num2 > num3){
 document.getElementById("result").innerHTML = "The biggest number was " +num2+ "!!";
  } else if (num3 > num2 && num3 > num1){
 document.getElementById("result").innerHTML = "The biggest number was "+num3+ "!!";
  }

}

What am I missing from my JavaScript code that will return a correct response no matter how large the number is?

  • 2
    Most likely those variables hold the string representation of the number not actual numbers. – MinusFour Aug 31 '18 at 01:55
  • 2
    Please add your code in the question - an image of code is not the same thing – Jaromanda X Aug 31 '18 at 02:02
  • Would it matter how big the string representation is? It works as long as a large number isn't being compared to a small number. Can you clarify? – N. Saunders Aug 31 '18 at 02:03
  • Please don't upload photos of your source code. Stackoverflow allows you to display your source code within your question. **Edit:** I have typed out the source code from your images but for future reference please remember to include the actual source code. – NewToJS Aug 31 '18 at 02:03
  • 1
    Possible duplicate of [Javascript string/integer comparisons](https://stackoverflow.com/questions/5630123/javascript-string-integer-comparisons) – MinusFour Aug 31 '18 at 02:06
  • NewToJS Thanks, I didn't know. I tried to do it but it said you already edited it. – N. Saunders Aug 31 '18 at 02:08
  • I'm asking that this question be not flagged as off-topic and reopened. I've modified the question so that the actual code is in block format instead of pictures. I feel that the question itself is a valid programming question that includes the original JavaScript code and can definitely benefit others, especially those who don't know that prompt() returns a string instead of an integer. Thanks! – N. Saunders Sep 06 '18 at 11:50

1 Answers1

2

A prompt() returns a string, not a number. Also, all HTML form fields return strings as well. So, if you try to compare the strings returned from them, they will do string comparisons. With a string comparison, 9 is greater than 800000.

console.log("9" > "800000");

You need to convert the strings to numbers.

var num1 = prompt("Enter number 1:");
var num2 = prompt("Enter number 2:");
var num3 = prompt("Enter number 3:");
console.log(typeof num1, typeof num2, typeof num3); // Promtps return strings!

// Convert the strings to numbers by prepending a + to it
num1 = +num1;
num2 = +num2;
num3 = +num3;

console.log(typeof num1, typeof num2, typeof num3); // Now, they are numbers

// Put numbers into an array
var nums = [num1, num2, num3];

// Sort the array (ascending) and report on the highest number
// which will now be last item in the array.
console.log("Highest number is:", nums.sort()[nums.length-1]);
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71