1

I got the sort and printout part of the program working, but i need to generate 100 random numbers between 1 and 100 and sort them. But I am not sure what is missing from the program to make it work correctly. I created an array to store the numbers in it and I made sure to specify the number of numbers I wanted and that it should be from 1-100.

<script>

             var arr = [];
             while(arr.length < 100){
            var r = Math.floor(Math.random() * 100) + 1;

            numbers.sort();  

            numbers.sort(function(a, b){
                return a - b;
            });
            document.write(numbers); 
            </script>
maker
  • 37
  • 5
  • @YevgenGorbunkov how? `Math.random()` will never return 1. – Pointy Mar 10 '20 at 15:25
  • 1
    Where is this `numbers` array declared. – Maheer Ali Mar 10 '20 at 15:26
  • You only need to sort the array once. You've got both calls to `.sort()` inside the `while` loop. Only the second `.sort()` is correct. Also you need to actually populate the array with values. e: oh and the code as posted is syntactically incorrect. – Pointy Mar 10 '20 at 15:26
  • See https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range – PM 77-1 Mar 10 '20 at 15:28
  • 1
    @maker I suggest to make an edit on your question and add the logic you are following in order to solve this problem. For example, the logical steps involved here would be first to create a random number then add it to an array, etc... I think getting help on this would be far more helpful rather someone handing you over the solution. – Alex Michailidis Mar 10 '20 at 15:28

4 Answers4

2

You had error in your while loop

var cnt = 0;
while (cnt < 100) {
  var r = Math.floor(Math.random() * 100) + 1;
  arr[cnt] = r;
  cnt++;
}

arr.sort(function(a, b) {
  return a - b;
});
document.write(arr);
Abey
  • 111
  • 3
  • 1
    `arr[cnt] = r` is unnecessary if you use `arr.push(r)`. – GetSet Mar 10 '20 at 15:38
  • @GetSet that's true but there's nothing wrong with using the index, and it saves a function call. – Pointy Mar 10 '20 at 15:53
  • Thats true @Pointy. ... Things would get a little sticky however if some conditional logic needed to be involved. (Which in this case is not the case). – GetSet Mar 10 '20 at 15:54
1

Try

[...Array(100)].map(x=> 1+Math.random()*100|0).sort((a,b)=>a-b);

a= [...Array(100)].map(x=> 1+Math.random()*100|0).sort((a,b)=>a-b);

console.log(a);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • The default `.sort()` comparison compares values as **strings**, not numbers. – Pointy Mar 10 '20 at 15:28
  • 2
    From the question I would guess that OP is a beginner. Throwing a bunch of ES6 and bit fiddling stuff at OP without any further explanation won't help... – Andreas Mar 10 '20 at 15:35
0

You can achieve this in a few lines using Array.from() method and { length: n } option like:

const numbers = Array.from({length:100},()=>Math.floor(100*Math.random())+1);
numbers.sort((a, b)=> a - b);
console.log( numbers )
.as-console-wrapper { max-height: 100% !important; top: 0; }
palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

Just modified your code, to add numbers into the array.

var numbers = [];
var i = 0;
while (i < 100) {
      var r = Math.floor(Math.random() * 100) + 1;
      numbers[i] = r;

     i++;
}
//numbers.sort();

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

document.write(numbers);
David
  • 173
  • 12