3

I'm trying to make a program that takes three numbers from the user and gives them the biggest and smallest, but sometimes the numbers are flipped (The biggest is switched with the smallest), and sometimes some numbers just get left out. Can anyone tell me what is happening?

const testArray = [
  prompt(`Pick a number`),
  prompt(`Pick a number`),
  prompt(`Pick a number`),
];

let max = testArray[0];
let min = testArray[0];

for (let i = 1; i < testArray.length; i++) {
  if (testArray[i] > max) max = testArray[i];
  if (testArray[i] < min) min = testArray[i];
}

console.log(`The biggest number you chose was ${max}`);
console.log(`The smallest number you chose was ${min}.`);

Somehow the numbers get flipped, or some numbers get left out.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • 3
    Hint: You need to convert those user-provided strings to numbers before doing the comparison – Trash Can Dec 18 '20 at 02:38
  • 9
    Because `'7' > '5'` – Robby Cornelissen Dec 18 '20 at 02:38
  • @RobbyCornelissen surely you know a dupe you can close this with... – Nick Dec 18 '20 at 02:44
  • This isn't sorted numerically, it's sorted ASCIIabetically. `1`, `11`, `111`, `2`, `21`, `22`, `222` is considered sorted. – tadman Dec 18 '20 at 02:47
  • 1
    @Nick I don't think this is a dupe. The question isn't asking how to turn a string into a number. It's asking about an unknown bug in a specific block of code. – ElectricShadow Dec 18 '20 at 02:48
  • @ElectricShadow It's asking why comparing numeric strings doesn't work as expected. – Nick Dec 18 '20 at 02:50
  • without even reading your code, I can tell you its sorting your numbers alphabetically. – Rick Dec 18 '20 at 05:28
  • Does this answer your question? [How to sort an array of integers correctly](https://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers-correctly) – Nick Dec 18 '20 at 11:38

3 Answers3

3

You need to convert the numbers into Integers or Float.

Use parseInt() to convert to integers or Use parseFloat() to convert to float values

let testArray = [
  parseInt(prompt(`Pick a number`)),
  parseInt(prompt(`Pick a number`)),
  parseInt(prompt(`Pick a number`))
];

let max = testArray[0];
let min = testArray[0];
for (let i = 1; i < testArray.length; ++i) {
  if (testArray[i] > max) max = testArray[i];
  if (testArray[i] < min) min = testArray[i];
}
alert(`The biggest number you chose was ${max}, and the smallest was ${min}.`);
// console.log(testArray.length);
console.log(min, max);
Harshana
  • 5,151
  • 1
  • 17
  • 27
  • 1
    If you have no control what users will input you should always use the second argument to `parseInt` -- `parseInt(prompt('..'), 10)` – slebetman Dec 18 '20 at 03:12
  • 1
    Thank you so much for helping me out, I spent 3 hours trying to figure this out. I'm just starting out coding and sometimes feel really dumb doing things like this, so thanks! – TheFishManJS Dec 18 '20 at 13:41
3

Why does your program think 72 is larger than 500?

Because -

  1. You are comparing between the strings "72" and "500", not between the numbers 72 and 500
  2. From the string comparison perspective "72" is greater than "500"

You can verify this with the following code -

// user inputs - 72, 123, 500
console.log(testArray);     // output: ["72", "123", "500"]
console.log("72">"500");    // output: true

How did this happen?

User inputs taken with prompt() are always read as strings.

How do you fix it?

As others have already mentioned, before comparing you have to convert the strings to numbers. You can do this while taking the inputs, like -

const testArray = [
  Number(prompt(`Pick a number`)),
  Number(prompt(`Pick a number`)),
  Number(prompt(`Pick a number`)),
];
atiyar
  • 7,762
  • 6
  • 34
  • 75
0

The fix here is to not only convert, but sort:

let sorted = testArray.map(v => parseInt(v, 10)).sort((a,b) => a-b);

let min = sorted[0];
let max = sorted[sorted.length - 1];
tadman
  • 208,517
  • 23
  • 234
  • 262