DojaCat
DojaCat

Reputation: 31

Trying to make my first JavaScript project work, full function is displayed in the "console" instead of the actual result I want from the code

I'm doing my first JS project which is a simple Rock, Paper, Scissor game that should work in the console.

I think my code is quite correct, but when I "console.log" my function (playRound) I get the following snippet in the "console": "You both played undefined It's a Draw!". I'm not sure what I'm missing so if someone could take a look then that would be very much appreciated!

Here is my script:

function computerPlay (){
    const choices = ["Rock", "Paper", "Scissor"];
    const randomChoice = choices[Math.floor(Math.random() * choices.length)];

    return(randomChoice);
    }


console.log(computerPlay()) //just to show "computerPlay" works

function playRound(playerSelection, computerPlay) { //This is the actual round

    if (playerSelection == computerPlay) {
        result = `You both played ${computerPlay} It's a Draw!`;
    } else if ((playerSelection == "Paper" && computerPlay == "Rock") ||
               (playerSelection == "Rock" && computerPlay == "Scissor") ||
               (playerSelection == "Scissor" && computerPlay == "Paper")) {
                   result = `${playerSelection} beats ${computerSelection} You Win!`;
               } else {
                   result = `${computerPlay} beats ${playerSelection} You Loose!`;
               }
            return (result);
        }

        let playerSelection = 'rock'

console.log(playRound()) // to test if "playRound" works

Thanks for checking!

Kind regards, A coding newbie

Upvotes: 1

Views: 91

Answers (2)

Swati
Swati

Reputation: 28522

You have not called the function you are just printing the content inside your function using console.log.

Try like below :

function computerPlay() {
  const choices = ["Rock", "Paper", "Scissor"];
  const randomChoice = choices[Math.floor(Math.random() * choices.length)];

  return (randomChoice);
}

computerPlay();//call it

function playRound(playerSelection, computerPlay) { //This is the actual round

  if (playerSelection == computerPlay) {
    result = `You both played ${computerPlay} It's a Draw!`;
  } else if ((playerSelection == "Paper" && computerPlay == "Rock") ||
    (playerSelection == "Rock" && computerPlay == "Scissor") ||
    (playerSelection == "Scissor" && computerPlay == "Paper")) {
    result = `${playerSelection} beats ${computerSelection} You Win!`;
  } else {
    result = `${computerPlay} beats ${playerSelection} You Loose!`;
  }
  return (result);
}

let playerSelection = 'rock'
playRound(playerSelection, playerSelection);//pass parameter in calling funciton
console.log(result) // to test if "result" is accurate

Upvotes: 1

TopWebGhost
TopWebGhost

Reputation: 335

Once you define the function, you should call it with parameters to see the result. The playRound function has 2 parameters, so please replace console.log(playRound) with console.log(playRound(playerSelection, computerPlay())).

Upvotes: 1

Related Questions