mercredi 5 mai 2021

Why is my function messing up my if statement? [closed]

I'm creating a simple Rock, Paper, Scissors console game, and I think one of my functions is messing with the rest of my code.

Previously I returned the random array selection as a variable, and the game was working fine.

Since creating the function computerPlay() and declaring the variable computerSelection, when I input to the prompt, it returns "undefined".

I feel I am missing something simple here but not sure why my code isn't working.

let selection = ["rock", "paper", "scissors"]
let playerScore = 0;
let computerScore = 0;

function computerPlay() {
  return selection[Math.floor(Math.random() * selection.length)];
}
let computerSelection = computerPlay();

console.log(computerPlay());

let playerSelection = prompt("Rock, Paper or Scissors? Choose Wisely!").toLowerCase();

function gamePlay(playerSelection) {
  if ((playerSelection === "rock" && computerSelection === 0) ||
    (playerSelection === "paper" && computerSelection === 1) ||
    (playerSelection === "scissors" && computerSelection === 2)) {
    return `Its a draw! We both chose ${playerSelection}. Your score: ${playerScore}/5  My Score: ${computerScore}/5`;
  } else if ((playerSelection === "rock" && computerSelection === 2) ||
    (playerSelection === "paper" && computerSelection === 0) ||
    (playerSelection === "scissors" && computerSelection === 1)) {
    return `You win! Your ${playerSelection} beats my ${computerSelection}. Your score: ${playerScore}/5  My Score: ${computerScore}/5`;
  } else if ((playerSelection === "rock" && computerSelection === 1) ||
    (playerSelection === "paper" && computerSelection === 2) ||
    (playerSelection === "scissors" && computerSelection === 0)) {
    return `You lose! My ${computerSelection} beats the your ${playerSelection}. Your score: ${playerScore}/5  My Score: ${computerScore}/5`;
  }
}

console.log(gamePlay(playerSelection));

Aucun commentaire:

Enregistrer un commentaire