First of all, it must be said that this may be a setState
issue as I have been having some trouble with that but I cannot work out why this while loop wont work.
code:
dealerTwisted = () => {
console.log(this.state.dealersOverallTotal, 'total on entry');
let looping = true;
while(looping){
console.log(this.state.dealersOverallTotal,'same');
if(this.state.dealersOverallTotal < 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);
console.log(total, 'tot');
this.setState({ dealersOverallTotal: total });
}
else{
console.log(this.state.dealersOverallTotal, 'logging as greater than 17');
if(this.state.playersOverallTotal > this.state.dealersOverallTotal){
console.log('player wins!');
looping = false;
}
else if (this.state.playersOverallTotal > this.state.dealersOverallTotal){
console.log('its a tie!');
looping = false;
}
else {
console.log('dealer wins!');
looping = false;
}
}
}
};
The way I think it is and the way I want it to work is as follows:
the function gets called, the loop is entered, you enter the if block if this.state.dealersOverallTotal
is less than 17. that total then gets updated and then you begin the loop again. you continue to enter the if block until you get to a number higher than 17. once you do you enter the else block and then whichever condition you hit in there, you break out.
interesting I have just debugged and found that this line (the fist console log) console.log(this.state.dealersOverallTotal, 'total on entry');
gives me the same number every time, i.e. not updating once inside. can anyone see why?