1

Hello is it possible to sort these random generated numbers in ascending order? I create randon 40 numbers out of 100 which are unique but when i try to sort them they are always out of order in the boxes. I tried sort or bubling or adding length but the result is always same. I know this is very basic but maybe there is something missing with my code... here is the final setup i tried...

  var randomNums = [];
  randomNums.sort(function (a,b) {
    return b - a;
  })
  for(var i = 0; i < 40 ; i++) {
    var temp = Math.floor(Math.random() * 100);
    if (randomNums.indexOf(temp) == -1) {
      randomNums.push(temp);
      document.getElementById("box"+i).innerHTML = randomNums [i];
    }
    else {
      i--;
    }
  }
 }```
4
  • 3
    move the sorting function down, after your for loop. You are sorting an empty array. Commented Jul 9, 2020 at 0:09
  • 1
    You will need to 1) generate the random numbers 2) sort them 3) and then display them. Commented Jul 9, 2020 at 0:10
  • 1
    Your sort function return b - a will sort in descending order, if you want ascending order you should return a - b Commented Jul 9, 2020 at 0:12
  • move the sorting function down, after your for loop. You are sorting an empty array. b - a would yield a descending order, use a - b. Also you have an extra closing brace. Commented Jul 9, 2020 at 0:15

3 Answers 3

1

Original

var randomNums = [];
  randomNums.sort(function (a,b) {
    return b - a;
  })
  for(var i = 0; i < 40 ; i++) {
    var temp = Math.floor(Math.random() * 100);
    if (randomNums.indexOf(temp) == -1) {
      randomNums.push(temp);
      document.getElementById("box"+i).innerHTML = randomNums [i];
    }
    else {
      i--;
    }
  }
 }
  1. If you need to display the numbers in ascending order, you would need another for loop after the array has been sorted.

Corrected

  var randomNums = [];
  //you have an empty array. this is not the right time to sort
  /*randomNums.sort(function (a,b) {
    return b - a;
  })*/
  for(var i = 0; i < 40 ; i++) {
    var temp = Math.floor(Math.random() * 100);
    if (randomNums.indexOf(temp) == -1) {
      randomNums.push(temp);
      document.getElementById("box"+i).innerHTML = randomNums [i];
    }
    else {
      i--;
    }
  }
  //#
  //you can now sort the array in ascending order after it has been populated
  randomNums.sort(function (a,b) {
    //the next line would yield a descending order
    //return b - a;
    return a - b;
  })
  //#create a new loop here to display the randomly generated numbers
  var len = randomNums.length;
  for(var i = 0; i < len; i++) {
    document.getElementById("box"+i).innerHTML = randomNums[i];
  }
 //}
Sign up to request clarification or add additional context in comments.

3 Comments

I know the problems with the original are stated in comments on the question, but you should re-state what the problems are and what you did to correct them as part of your answer. Comments can be - and often are - removed. I like how you comment-out the original code in your corrected version.
@StephenP That's true. I thought this was a continuation of the last comment I made :)
Thank you i was pretty sure i had to sort before looping them this works fine =)
0

A possible solution could be e.g.:

// create random numbers
const size = 40
const randomNums = []
while (randomNums.length < size) {
  const randomNum = Math.floor(Math.random() * 100)
  if (randomNums.indexOf(randomNum) === -1) {
    randomNums.push(randomNum)
  }
}

// sort random numbers
const randomNumsSorted = randomNums.concat().sort(
  (a, b) => a - b
)

// output random numbers to html
for (let i = 0; i < size; i++) {
  document.getElementById(`box${i}`).innerHTML = randomNumsSorted[i]
}

1 Comment

Thank you, this also works fine, even the coding seems more practical i guess it would be easier if i started this way from the scratch ... thanks again =)
0

You are sorting the array before insert the numbers. Move:

randomNums.sort(function (a,b) {
 return b - a;
})

after the for loop.

1 Comment

Thank you i was pretty sure i had to sort before looping them this works fine =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.