1

I was trying to build an MCT calculator in javascript, however during the creation of the median, I kept getting errors. This is my code: (Keep in mind I am relatively new to javascript)

var min = prompt("Enter minimum value");
var max = prompt("Enter maximum value");
if (min > max) {
    alert("Error:\n\n\nMinimum value '" + min + "' is greater than Maximum value '" + max + "'");
} else {
    var a = (max - min);
    alert("variable a is " + max + " - " + min + " = " + a);
    var b = a / 2;
    alert("variable b is " + a + " / 2 = " + b);
    var c = min + b;
    alert("variable c is " + min + " + " + b + " = " + c);
    alert("Range is " + a + "\n\nMedian is " + c + "\n\nMin is " + min + "\n\n Max is " + max);
}

If you type in 5 for minimum value and 10 for maximum value, it will tell you 5 is larger than 10, but it isn't.. And if you type 10 for minimum value and 15 for maximum value, it will have the median be 102.5 rather than 12.5 I do not get this at all, please help :)

Fizzy
  • 5
  • 6
  • @Stijn: That the **answer** to another question also answers this one doesn't imply that this **question** is a duplicate. – Scott Sauyet Apr 28 '14 at 21:18
  • @ScottSauyet The question is also the same if you ignore the story surrounding the actual question. – user247702 Apr 28 '14 at 21:27
  • @Stijn: obviously enough people agreed with you to close the question, but I still don't think that a question asking, "Why is arithmetic not working for my numbers?" is even close to a duplicate of one asking, "Why aren't my strings being treated as numbers?" The latter is already much closer to a solution than the former. – Scott Sauyet Apr 29 '14 at 10:24
  • @ScottSauyet I gave this some more thought and I agree now. Just casted the final reopen vote. – user247702 May 01 '14 at 19:34
  • @Stijn: Cool. I think a good answer (or comment) to this sort of thing could easily be just a link to a different question whose answer also answers this one, or a direct link to that answer. But I'd rather not frighten off newbies unnecessarily. Thanks! – Scott Sauyet May 03 '14 at 13:47

2 Answers2

4

Try using numbers, not strings:

var min = parseInt(prompt("Enter minimum value"), 10);
var max = parseInt(prompt("Enter maximum value") , 10);

Or, if you want to allow for floats:

var min = parseFloat(prompt("Enter minimum value"));
var max = parseFloat(prompt("Enter maximum value"));

This because strings are compared lexicographically, using their numerical character codes, rather than the value of the number itself.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
1

The prompt function returns a string, and strings are compared according to the Unicode code points of each character in the string (also called lexicographical or alphabetical order). The code point of "5" is greater than the code point of "1", so the string "5" actually is greater than the string "10" (i.e. comes after it in lexicographical ordering) .

Try parsing the inputs like this:

var min = parseFloat(prompt("Enter minimum value"));
var max = parseFloat(prompt("Enter maximum value"));
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331