javascriptfunctionparameter-passing

Why isn't a function using returned arguments from previous functions?


I am a beginner in The Odin project JavaScript part of the foundations course and doing The Rock&Paper&Scissors assignment

My playRound function isn't working properly, it just doesn't read the parameters as it should to give me the correct result in the game. Why is this happening?

Here is my code:

let humanScore = 0;
let computerScore = 0;

function getComputerChoice() {
  let computerRound = Math.random();
  if (computerRound <= 0.33) {
    return "rock";
  } else if (computerRound >= 0.34 && computerRound < 0.66) {
    return "paper";
  } else {
    return "scissors";
  }
}

console.log(getComputerChoice());

function getHumanChoice() {
  let humanRound = prompt("Rock, Paper or Scissors? ").toLowerCase();
  if (humanRound == "rock") {
    return "rock";
  } else if (humanRound == "paper") {
    return "paper";
  } else if (humanRound == "scissors") {
    return "scissors";
  } else {
    alert("enter 'rock, paper or scissors'");
  }
}
console.log(getHumanChoice());

const computerSelection = getComputerChoice();
const humanSelection = getHumanChoice();

function playRound(getComputerChoice, getHumanChoice) {
  if (
    (computerSelection == "paper" && humanSelection == "rock") ||
    (computerSelection == "scissors" && humanSelection == "paper") ||
    (computerSelection == "rock" && humanSelection == "scissor")
  ) {
    return `You lose ${computerSelection} beats ${humanSelection}`;
  } else if (computerSelection === humanSelection) {
    return `Draw`;
  } else return `You win`;
}

console.log(playRound(computerSelection, humanSelection));

I've read through my lessons again, re-written the code but still stuck on the same issue


Solution

  • getComputerChoice()

    With parentheses, this means to execute the function. The code in the function runs, and then a value is returned, and you can decide what to do with the value. For example, you have this:

    console.log(getComputerChoice());

    That will take the return value from getComputerChoice() and log it, but after that nothing else gets done with the return value.

    getComputerChoice

    If there's no parentheses, then you aren't calling the function, you're just referring to it. So when you do this:

    const computerSelection = getComputerChoice;

    All that's doing is saying that computerSelection should be the same function as getComputerChoice. The function doesn't actually get called and nothing gets computed.

    What you probably meant to do was this:

    const computerSelection = getComputerChoice();
    const humanSelection = getHumanChoice();
    
    console.log(computerSelection);
    console.log(humanSelection);
    

    The line const computerSelection = getComputerChoice(); will call the function, get the result, and then assign that result to computerSelection. Since you now have it stored in a variable you can use it later, logging it out, or passing it into playRound