For the life me of I can't figure out why indexOf cant find the number in the array. It keeps returning -1. My goal is to ban a noun and put it into a ban array list, the list must be unique. So each element in the array must be different. Since I keep getting -1, my while loop is never executing.
Can anyone please explain to me what I'm doing wrong!
if( useA < 101 && totalAs < 5){
article1Num = 4; // A
noun1Num = [Math.floor(Math.random() * 5)];
//^^^ random number to try use
//--------
// Code to check if number is ban
alert("Test Noun1Num is " + noun1Num);
alert(bannedNounsTest.indexOf(noun1Num));
//^^^^ITS ALWAYS -1 !!!!!!! EVEN if there is a match!
while (bannedNounsTest.indexOf(parseInt(noun1Num)) >= 0 ) {
// ^^^searching the value of the current noun in ban, -1 if none
alert("In Loop and noun1Num is " + noun1Num);
noun1Num = [Math.floor(Math.random() * 5)];
// ^^looking for new number not in index while
}
//----------
bannedNounsTest.push(noun1Num); // put in ban list
totalAs++;
Aucun commentaire:
Enregistrer un commentaire