mardi 30 novembre 2021

Splicing random elements and forming a new Array out of them

I didn´t find this exact question yet, sorry if it already exists!

I´ve got some trouble with homework, Ive got an array from 1 to 50 and want to choose 6 random numbers out of it. I´m doing that with array.splice([Math.floor(Math.random()*array.length)],1)

which works fine for 1 number, but when i interchange the 1 at the end to a 6, it just chooses a random index and proceeds to splice 6 items in a row. Doing a for loop to get a random number 6 times also works but i can´t think about a proper solution to display 6 item arrays at the end

My goal is to create a new array containing exactly 6 NON REPEATING random items and on top of that, do that as many times as you input in the beginning with a prompt and display the arrays in the console, looking something like this:

[5,7,8,10,12,41] [1,10,22,23,40,42] [1,2,17,19,38,45]

I´d greatly appreciate any help, thanks in advance!




Aucun commentaire:

Enregistrer un commentaire