I want to make it so that each one of these four variables always takes a different "name" value taken from the array. I wanted to write it so that it check whether the variable is equal to any of the other ones, and if it is, it re-randomizes it to be different. So far I've got this:
const arr = [{name:"a"},{name:"b"},{name:"c"},{name:"d"},{name:"e"},{name:"f"}]
function myFunction()
{
let var1 = arr[Math.floor(Math.random() * arr.length)].name;
let var2 = arr[Math.floor(Math.random() * arr.length)].name;
let var3 = arr[Math.floor(Math.random() * arr.length)].name;
let var4 = arr[Math.floor(Math.random() * arr.length)].name;
while (var1 == var2 || var1 == var3 || var1 == var4)
{
var1 = arr[Math.floor(Math.random() * arr.length)].name;
}
while (var2 == var1 || var2 == var3 || var2 == var4)
{
var2 = arr[Math.floor(Math.random() * arr.length)].name;
}
while (var3 == var2 || var3 == var1 || var3 == var4)
{
var3 = arr[Math.floor(Math.random() * arr.length)].name;
}
while (var4 == var2 || var4 == var3 || var4 == var1)
{
var4 = arr[Math.floor(Math.random() * arr.length)].name;
}
}
But it not only doesn't work, I've also go a problem where the variables can still be equal, because while the first "while" randomizes them to be different from others, the next "while"'s can set another variable do be equal to it.