I have really specific question. I spent many hours to make it work, but I wasn't successful.
Here is piece of my code, how it looks now:
var mainNames = ["James", "Robert", "John", "Michael", "William", "David", "Peter", "Joseph"];
var genNames = [];
var exceptionNames = ["James", "Michael", "David", "Robert"];
for (var i = 0; i < 4; i++)
{
var idx = Math.floor(Math.random() * names .length);
genNames.push(names [idx]);
}
What I need to do?
mainNames is the main list from which I need to generate 4 unique names, without repetitions. Then, if in the generated list will be found 2 or more exceptionNames, repeat the generation of the mainNames list until the final generated list contain maximum of 1 exceptionNames and no repetitions of the mainNames.
Note: Generated list can contain 0 exceptionNames also, because the generation is random and does not have to generate exceptionNames as an extra names, because they already exists in the mainNames list.
How do I print the final list?
${genNames[0]}, ${genNames[1]}, ${genNames[2]}, ${genNames[3]}
Here are some possible correct outputs:
James, William, John, Peter
Michael, William, Peter, John
Robert, John, William, Peter
David, John, William, Peter
Joseph, Peter, John, William
Note: As you can see in the possible correct outputs are no repetitions of the mainNames and no repetitions of the exceptionNames. And that’s how the above mentioned code should work.
Thanks for possible solutions/answers!