alias
alias

Reputation: 25

javascript weird console logs

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 ]

Upvotes: 1

Views: 413

Answers (4)

Sushant Agrawal
Sushant Agrawal

Reputation: 11

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);

Upvotes: 0

jsadev.net
jsadev.net

Reputation: 2590

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();

Upvotes: 2

Ayesha Mundu
Ayesha Mundu

Reputation: 1095

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!

Upvotes: 0

Furkan Selcuk
Furkan Selcuk

Reputation: 55

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>

Upvotes: 0

Related Questions