jeudi 6 juin 2019

Basic level non-repetitive random in JS

I'm trying to make a quiz, and I need 5 random questions, to make it work I created random variables(rnd-rnd5) because I needed these variables in two functions. I need to make non-repetitive variables, but the solution below doesn't work. I need "basic" solutions if possible, because our teacher wants us to have a code that is "on our level".

var rnd = [Math.floor(Math.random() * 29) + 0];
var rnd2 = [Math.floor(Math.random() * 29) + 0];
while (rnd2 !== rnd){
    rnd2 = Math.floor(Math.random() * 29) + 0;
}

var rnd3 = [Math.floor(Math.random() * 29) + 0];
while (rnd3 !== rnd && rnd3 !== rnd2){
    rnd3 = Math.floor(Math.random() * 29) + 0;
}

var rnd4 = [Math.floor(Math.random() * 29) + 0];
while (rnd4 !== rnd && rnd4 !== rnd2 && rnd4 !== rnd3){
    rnd4 = Math.floor(Math.random() * 29) + 0;
}
var rnd5 = [Math.floor(Math.random() * 29) + 0];
while (rnd5 !== rnd && rnd5 !== rnd2 && rnd5 !== rnd3 && rnd5 !== rnd4){
    rnd5 = Math.floor(Math.random() * 29) + 0;
}




Aucun commentaire:

Enregistrer un commentaire