lundi 9 novembre 2020

JS RPS Best-Of-5 function has to run an additional time to give winner result

I'm very new to JS, day 7 of learning/coding for an hour or 2 per day. I am making a JS RPS game, everything works well, except when I'm trying to run a function for a Best of 5 game. it counts everything properly, but, when it gets to 5, I have to run the function an additional time to get the Winner result.

I understand why it's doing this, because before the function runs the value is < 5. I'm just not sure how to fix it, I've searched around a lot, but probably am not using the proper search terms.

Code:

const playGame = function (playerScore, cpuScore) {
  if (playerScore === 5) {
    console.log(`You Won The Best Of 5, ${playerScore} to ${cpuScore}.`);
  } else if (cpuScore === 5) {
    console.log(`You Lost The Best Of 5, ${playerScore} to ${cpuScore}.`);
  } else if (playerScore < 5 && cpuScore < 5) {
    playRound(cpuChoice, playerChoice);
    console.log('next round');
  }
};

Aucun commentaire:

Enregistrer un commentaire