This while loop works sometimes and sometimes it doesn't. can anyone see why? I feel like it should start looping, and keep looping till the dealers total is greater than 17 and hit the else block. once it goes into the else block it should update the outcome value. however, sometimes it jumps into the else block and then jumps straight to the console.log(outcome) and tells me it is undefined. sometimes it will return the correct outcome. e.g. it outputs 'dealer wins!' I feel like I have met every condition? I have even put in console.logs and the line dealersTotal = total gets updated correctly. why is it not continuously looping?
dealerTwisted = () => {
let dealersTotal = this.state.dealersOverallTotal;
let playersTotal = this.state.playersOverallTotal;
let looping = true;
let outcome = '';
while(looping){
if(dealersTotal < 17){
this.deal2Dealer();
let dealersDeck = this.state.dealersDeck;
let newDealersDeckTotal = [];
for (var i=0; i < dealersDeck.length; i++){
newDealersDeckTotal.push(dealersDeck[i].rankValue)
}
let total = newDealersDeckTotal.reduce(function(a, b) {
return a + b;
},
0);
dealersTotal = total;
}
else {
if(dealersTotal > 21){
outcome = 'player wins!';
break;
}
else if(playersTotal > dealersTotal){
outcome = 'player wins!';
break;
}
else if (playersTotal == dealersTotal){
outcome = 'tie!';
break;
}
else if (dealersTotal > playersTotal){
outcome = 'dealer wins!';
break;
}
else {
console.log('got here');
break;
}
}
console.log(outcome);
this.setState({resultOutcome: outcome})
break;
}
};
Aucun commentaire:
Enregistrer un commentaire