Skip to content
Advertisement

Ties are still running win messages?

I’ve been messing around with a rock, paper scissors project that codecademy gets you to try, I’m pretty happy with it but I have one problem! When the result is a tie, it logs the tie message (perfect!) but also logs the win message linked with the result! How do I get it to ONLY log the tie message? Here’s the code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "gun") {
    return userInput;
  } else {
    console.log(`Sorry! But ${userInput} is an illegal weapon in the bloodythirsty sport of Rock, Paper, Scissors!`)
  }
}

const getComputerChoice = () => {
  const ranNum = Math.floor(Math.random() * 3);
  switch (ranNum) {
    case 0:
      return "rock";
    case 1:
      return "paper";
    case 2:
      return "scissors";
  }
};

const determineWinner = (getUserChoice, getComputerChoice) => {
  if (getComputerChoice === getUserChoice) {
    console.log("It seems it's a tie! You've matched wits!");
  }
  if (getUserChoice === "rock") {
    if (getComputerChoice === "paper") {
      return "The computer wins! It has gift wrapped your weapon.";
    } else {
      return "You beat the computer! They immediately begin crafting bigger scissors.";
    }
  }

  if (getUserChoice === "paper") {
    if (getComputerChoice === "scissors") {
      return "The computer wins! They claim arts and crafts time as a reward."
    } else {
      return "You beat the computer! Their puny stone was no match for your paper aeroplane to the eye!"
    }
  }

  if (getUserChoice === "scissors") {
    if (getComputerChoice === "rock") {
      return "The computer wins! You won't be cutting straight lines any time soon..."
    } else {
      return "You beat the computer! You cause emotional damage by destroying their robot child's drawing. You're a monster."
    }
  }

  if (getUserChoice === "gun") {
    if (getComputerChoice === "rock" || getComputerChoice === "paper" || getComputerChoice === "scissors") {
      return "You win. But at what cost?"
    }
  }
  if (getUserChoice === undefined) {
    return "Come back when you're ready to take this game seriously."
  }
}
//Enter your choice below in getUserChoice brackets
const playGame = () => {
  let userChoice = getUserChoice("rock");
  let computerChoice = getComputerChoice();
  if (userChoice !== undefined) {
    console.log(`You have chosen ${userChoice} as your weapon.`);
  }
  if (userChoice !== undefined) {
    console.log(`The computer has brought ${computerChoice} to a ${userChoice} fight.`);
  }
  console.log(determineWinner(userChoice, computerChoice))
}

playGame();

Advertisement

Answer

Instead of logging the tie message, you should return it, leaving the logging to the caller:

if (getComputerChoice === getUserChoice) {
  return "It seems it's a tie! You've matched wits!";
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement