Tim Dowd
Tim Dowd

Reputation: 205

Javascript score counter keeps adding no matter the result

massive noob here.

I'm making a rock paper scissor game in which one of the functions called 'game', counts the score between the player and the computer over 5 rounds.

The problem I am having is that no matter whether the player wins loses or draws, +1 is added to the score every time, instead of -1 for a loss and maintaining the score on a draw.

function game() {
  var roundCount = 0;
  var score = 0;

  while (roundCount < 5) {
    playRound(prompt("Rock, Paper or Scissors?"), computerPlay());

    if (resultMessage == "You Win. Rock beats Scissors" || "You Win. Paper beats Rock" || "You Win. Scissor beats Paper") {
      score++;
    } else if (resultMessage == "Draw") {
      score = score;
    } else {
      score--;
    }
    console.log(score)
    roundCount = roundCount + 1;
  }
  return score;
}

console.log(game());

Upvotes: 0

Views: 431

Answers (3)

matbou
matbou

Reputation: 159

You could implement the playRound() function by returning a value of -1,0,1 (-1=>lose, 0=>draw, 1=>win). You can then directly add this to the score and it will save you comparing string values. I've included a code sample that demonstrates this, but you would need to replace the playRound(choice) function with your own logic to generate a computer choice, test for win/draw/lose, and return the correct response. Goodluck :)

function playRound(choice) {
  //replace this random win/draw/lose function with play logic
  let randomResult = Math.floor((Math.random() * 3) - 1);
  return randomResult;
}

function game() {
  //init round and score variables
  let roundCount = 0;
  let score = 0;

  //play 5 rounds
  while (roundCount < 5) {
    //get users choice
    let userChoice = prompt("Rock, Paper, or Scissors?");

    //play round and get result (-1 => loss, 0 => draw, 1=> win)
    score += playRound(userChoice);

    //print current score and increment round count
    console.log("Current Score: " + score);
    roundCount++;
  }

  //print final result (no need to check for draw as we have an odd number of rounds)
  console.log("Final Score: " + score);
  if (score < 0) {
    console.log("You lost the game :(");
  } else {
    console.log("You won the game :)");
  }
}

//start game
game();

Upvotes: 0

David Ibl
David Ibl

Reputation: 911

This does not work:

if(resultMessage == "You Win. Rock beats Scissors" || "You Win. Paper beats Rock" || "You Win. Scissor beats Paper") {
    score++;

}    

You have to compare each value:

if(resultMessage === "You Win. Rock beats Scissors" || resultMessage === "You Win. Paper beats Rock" || resultMessage === "You Win. Scissor beats Paper") {
    score++;

}    

Upvotes: 1

Anthony L
Anthony L

Reputation: 2169

This will always return true:

resultMessage == "You Win. Rock beats Scissors" || "You Win. Paper beats Rock" || "You Win. Scissor beats Paper"

Because the second and third dysjuncts are strings, which are always true.

Upvotes: 1

Related Questions