I have a task to select three unique names from an array and store them in a new array. However, I am facing an issue where the same names keep getting randomized instead of finding new ones.
If you want to see the code in action, check out this jsfiddle script.
Code:
findStudentBtn.onclick = findStudent;
function findStudent() {
var studArray = ["John","Daniel","Hans","Lars","Tom","Amanda","Jane","Sarah"];
for (i=0; i<3; i++) {
if (i === 1) {
var randomStud1 = studArray[Math.floor(Math.random() * studArray.length)];
msg8.innerHTML += randomStud1 + ", ";
}
if (i === 2) {
var randomStud2 = studArray[Math.floor(Math.random() * studArray.length)];
msg8.innerHTML += randomStud2 + ", ";
}
if (i === 3) {
var randomStud3 = studArray[Math.floor(Math.random() * studArray.length)];
msg8.innerHTML += randomStud3 + ", ";
}
if (randomStud1 == randomStud2 || randomStud2 == randomStud3 || randomStud1 == randomStud3){
msg8.innerHTML = "";
findStudent();
}
}
}