0

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());

2
  • Give yourself some credit, noobs don't make rock paper scissors :) Commented Mar 25, 2018 at 21:05
  • I'm disappointed this isn't paper/rock/scissors/lizard/spock. Commented Mar 25, 2018 at 21:09

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much
1

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++;

}    

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.