0

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;
}
levi T
  • 21
  • You are generating an *array* with a single random number as a member. It seems like you don't want that, so just drop all `[` and `]` from the code and it should work. – VLAZ Jun 06 '19 at 11:59
  • create an array of your numbers, 0 to 29, shuffle it, then pick first 5 items, simple – Lawrence Cherone Jun 06 '19 at 11:59
  • Also, your logic is flipped, you need `===` instead of `!==` – VLAZ Jun 06 '19 at 12:02
  • "but the solution below doesn't work" if that text appears in a question and the OP doesn't explain how it isn't working, the question is not complete. It's not enough to just put code in. – zero298 Jun 06 '19 at 13:00

2 Answers2

0

You have few problems:

  1. You are generating an array with a single member which is a random number. Arrays are always not equal to other arrays and any other value than themselves. It appears as if you want a plain variable, so you just need to remove the [ and ] surrounding the Math.floor calls.

  2. You also have the checks backwards - you are generating new numbers if the the current one is not equal to the old one. Which means you are generating until the two are the same. You just need to do the opposite check === instead.

  3. You are checking using AND && where you need to check using OR to catch if any of the previous numbers was matched.

This results in the working code:

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

console.log(rnd, rnd2, rnd3, rnd4, rnd5)

With that said, you can generate random non-repeating numbers easier if you generate non-random numbers first, then jumble them and pick however many you need.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
0

Create an array of numbers

var numbers = [];
for(var i=0; i<30; ++i) numbers.push(i);

Shuffle the numbers by going from 0 to 29 (i), in each cycle pick a random number 0-29 (j) and swap the values at indexes i, j.

for(var i=0; i<30; ++i) {
  var j = Math.floor(Math.random()*30);
  var tmp = numbers[i];
  numbers[i] = numbers[j];
  numbers[j] = tmp;
}

First n numbers (5) are random and unique:

for(var i=0; i<5; ++i) {
  console.log(numbers[i]);
}
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169