1

I've got a assignment to make function that gives you a sorted array with 6 random numbers from 1 to 45. None of the array values should equal to one another.

I thought about a solution that would work in Java but the JavaScript console logs I get are pretty confusing. Could anyone help me out?

"use strict";

var numbers = [];
for(var x = 1; x <46;x++){
    numbers.push(x);
}


function LottoTipp(){
    var result = [];

    for(var i = 0; i <6; i++){
      var randomNum = Math.round(Math.random()* 45);
      var pushed = numbers[randomNum];
      result.push(pushed);
      numbers.splice(randomNum)
    }

    return console.log(result) + console.log(numbers);

}

LottoTipp(); 

the console logs

[ 34, 7, undefined, undefined, undefined, undefined ]

[ 1, 2, 3, 4, 5, 6 ]
alias
  • 25
  • 4
  • 1
    You don't use `splice`correctly – jhamon Nov 13 '19 at 10:29
  • Also, since you added it back, how is Java relevant? – Federico klez Culloca Nov 13 '19 at 10:29
  • your `splice` is messed up, it should be `splice(index, number of delete items)` – joe Nov 13 '19 at 10:30
  • just add `splice(randomNum, 1)` and it outputs (6) [33, 7, 25, 24, 26, 28] (39) [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 27, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45] – joe Nov 13 '19 at 10:31
  • still, your logic isnt right since random numbers could eventually produce the same arrays. also, splice modifies the original array, so you need to make a copy before you start splicing it. – joe Nov 13 '19 at 10:33
  • @joe thanks! Also as the numbers array lenght gets smaller the randomNum could be longer than the numbers array. so my solution would be instead of the *45 -> numbers.lenght() but now the console says TypeError: numbers.lenght is not a function – alias Nov 13 '19 at 10:39
  • It has to be `length` not `lenght`. – Adder Nov 13 '19 at 10:42
  • @Adder well its a typo in the comment its right in the code though. same error – alias Nov 13 '19 at 10:44
  • Well `length` is not a function but a field, remove the `()` after it. – Adder Nov 13 '19 at 10:46
  • @Adder thank you, i make stupid mistakes all the time – alias Nov 13 '19 at 10:49

4 Answers4

2

There were three problems:

  • If you want to remove one item of an array you have to splice it by the items index and give a deletecount.

    In your case: numbers.splice(randomNum, 1);

  • You have to use Math.floor instead of Math.round, because Math.floor always down to the nearest integerer, while Math.round searches for the nearest integer wich could be higher than numbers.length.

  • After removing one item the length of the array has been changed. So you have to multiply by numbers.lenght instead of 45.

    In your case: var randomNum = Math.floor(Math.random()* numbers.length);

"use strict";

var numbers = [];
for(var x = 1; x < 46; x++){
    numbers.push(x);
}


function LottoTipp(){
    var result = [];

    for(var i = 0; i < 6; i++){
      var randomNum = Math.floor(Math.random()* numbers.length);
      var pushed = numbers[randomNum];
      result.push(pushed);
      numbers.splice(randomNum, 1);
    }

    return console.log(result) + console.log(numbers);

}

LottoTipp();
jsadev.net
  • 2,800
  • 1
  • 16
  • 28
  • 1
    Shouldn't it be `var randomNum = Math.floor(Math.random()* numbers.length);` – Adder Nov 13 '19 at 10:41
  • thanks a lot !well i did that but now the console shows TypeError: numbers.lenght is not a function – alias Nov 13 '19 at 10:42
  • thats because it has to be `length` and not `lenght` :) – jsadev.net Nov 13 '19 at 10:46
  • 1
    I have to insist that `Math.floor` is correct. `math.round` leads to `undefined` entry, if you click "Run Code Snippet" often enough. – Adder Nov 13 '19 at 10:53
  • got one more point only for your information: you dont need to use `.indexOf` in this case, since `randomNum` is already the index of the number you've pushed. My Fault, updated again. :) – jsadev.net Nov 13 '19 at 11:06
0

If you only want an array with random unique numbers I would suggest doing it like this:

<script>
     var array = [];
     for(i = 0; i < 6; i++) {
        var number = Math.round(Math.random() *45);
        if(array.indexOf(number) == -1) { //if number is not already inside the array
            array.push(number);
        } else { //if number is inside the array, put back the counter by one
            i--;
        }
     }
     console.log(array);
</script>
  • While this could be a nice solution, the OP will not get whats going wrong with its own code. So there will be no learning effect. – jsadev.net Nov 13 '19 at 10:56
  • @J.Sadi is right but i appreciate the help and perspective on another way to do this assignment. thank you :) – alias Nov 13 '19 at 11:05
0

There is no issue with the console statement the issue is that you are modifying the numbers array in your for loop. As you are picking the random number between 1-45 in this statement:-

var randomNum = Math.round(Math.random()* 45);

and you expect that value would be present in the numbers array at that random index. However you are using array.splice() and providing only first parameter to the function. The first parameter is the start index from which you want to start deleting elements, find syntax here. This results in deleting all the next values in the array.Therefore if you pick a random number number say.. 40, value at numbers[40] is undefined as you have deleted contents of the array.
enter image description here

if you want to generate unique set of numbers follow this post.

hope it helps!

Ayesha Mundu
  • 1,075
  • 2
  • 11
  • 18
0

Just add the number in the result if it is unique otherwise take out a new number and then sort it. Here is an implementation:

let result = []
while(result.length < 6) {
    let num = Math.round(Math.random() * 45);
    if(!result.includes(num)) {
        result.push(num);
    }
}
result.sort((a,b) => {
    return parseInt(a) - parseInt(b);
});
console.log(result);
  • 1
    Welcome to [so]. While it may answer the question, try your best to not just paste code, but give explanation along. – PJProudhon Nov 13 '19 at 10:56
  • Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Nov 13 '19 at 11:25