Reputation: 115
The first switch statement runs fine when initializePlayers is 4 but when the second if statements condition is met it will console.log("was < 4") but then skip my switch statement? I'm confused about this because if initializePlayers is < 4 shouldn't it run that case in the switch statement?
let humanPlayers = [];
//initialize the participants
initializePlayers = prompt("How many players? (1-4)");
console.log("value of initalPlayer" , initializePlayers);
if (initializePlayers == 4) {
for (i = 0; i <= initializePlayers; i++) {
console.log("the number was four!");
switch (i) {
case 0:
humanPlayers.push(Player1);
console.log(0);
break;
case 1:
humanPlayers.push(Player2);
console.log(1);
break;
case 2:
humanPlayers.push(Player3);
console.log(2);
break;
case 3:
humanPlayers.push(Player4);
console.log(3, humanPlayers);
break;
}
}
}
if (initializePlayers < 4) {
console.log("was < 4");
//initializing human objects first
switch (initializePlayers) {
case 1:
humanPlayers.push(Player1);
console.log(1, "1 human");
break;
case 2:
humanPlayers.push(Player2);
console.log(2, "2 humans");
break;
case 3:
humanPlayers.push(Player3);
console.log(3, "3 humans");
break;
}
}
Upvotes: 1
Views: 88
Reputation: 89507
prompt
returns a string, so you should have strings instead of numbers as your cases in each switch
.
if (initializePlayers < 4) {
console.log("was < 4");
//initializing human objects first
switch (initializePlayers) {
case '1':
humanPlayers.push(Player1);
console.log(1, "1 human");
break;
case '2':
humanPlayers.push(Player2);
console.log(2, "2 humans");
break;
case '3':
humanPlayers.push(Player3);
console.log(3, "3 humans");
break;
}
}
Upvotes: 1