Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

303
Views
The variables inside the functions can't be called

I'm doing this rock, paper, and scissors game, however, my last function it's not working properly, it won't execute that block of code, and what it's wrong with the variables in the last function, they cannot be called, I've tried to change the parameters, I also debugged the code and to the extent I notice just the last function it's not working properly

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
    return userInput;
  } else {
    console.log('Enter a valid option')
  };
};

function getComputerChoice(any) {
  return Math.floor(Math.random() * any)
}
switch (getComputerChoice(3)) //userInput
{
  case 0:
    console.log('rock');
    break;
  case 1:
    console.log('paper');
    break;
  case 2:
    console.log('scissors');
    break;
  default:
    console.log('Select a valid option')
}

function determineWinner(getUserChoice, getComputerChoice) {
  if (getUserChoice === getComputerChoice) {
    return 'The game was a tie'
  }
  if (getUserChoice === 'rock') {
    if (getComputerChoice === 'paper') {
      return 'The computer won!'
    } else {
      return 'You won'
    }
  }
  if (getUserChoice === 'scissors') {
    if (getComputerChoice === 'rock') {
      return 'Computer won!'
    } else {
      return 'You won!'
    }
  }
}
const playGam = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}
playGam();

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

As posted, getComputerChoice just does random math and multiplies by a (sometimes missing) param. Consider changing to return a random choice from the set of ['rock', 'paper', 'scissors']...

function getComputerChoice() {
  const choices = ['rock', 'paper', 'scissors'];
  return choices[Math.floor(Math.random() * choices.length)]
}

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
    return userInput;
  } else {
    console.log('Enter a valid option')
  };
};

function getComputerChoice() {
  const choices = ['rock', 'paper', 'scissors'];
  return choices[Math.floor(Math.random() * choices.length)]
}

switch (getComputerChoice(3)) //userInput
{
  case 0:
    console.log('rock');
    break;
  case 1:
    console.log('paper');
    break;
  case 2:
    console.log('scissors');
    break;
  default:
    console.log('Select a valid option')
}

function determineWinner(getUserChoice, getComputerChoice) {
  if (getUserChoice === getComputerChoice) {
    return 'The game was a tie'
  }
  if (getUserChoice === 'rock') {
    if (getComputerChoice === 'paper') {
      return 'The computer won!'
    } else {
      return 'You won'
    }
  }
  if (getUserChoice === 'scissors') {
    if (getComputerChoice === 'rock') {
      return 'Computer won!'
    } else {
      return 'You won!'
    }
  }
}
const playGam = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}
playGam();

We could also improve the code in a few other ways...

// make this global
const choices = ['rock', 'paper', 'scissors'];
const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' };

// general purpose fn to validate
const validateChoice = choice => {
  return choices.includes(choice.toLowerCase());
};

// a tightly specified thing to determine winner
const determineWinner(userChoice, computerChoice) {
  if (computerChoice === userChoice) return 'tie';
  return beats[userChoice] === computerChoice ? 'user win' : 'computer win';
}
 
about 4 years ago · Juan Pablo Isaza Report

0

You're not setting a variable value with your swtich statement. You need to return what's the computer's choice, so I've put your switch statement inside the getComputerChoice function and then it returns the output and you can use it in your playGam function. I've updated that as well to display the winner in every outcome.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
    return userInput;
  } else {
    console.log('Enter a valid option')
  };
};

function getComputerChoice(any) {
  const randomNo = Math.floor(Math.random() * any)
  switch (randomNo) //userInput
  {
    case 0:
      return 'scissors';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
      return 'rock';
      break;
    default:
      return 'Select a valid option';
  }
}


function determineWinner(getUserChoice, getComputerChoice) {
  if (getUserChoice === getComputerChoice) {
    return 'The game was a tie'
  }
  switch (getUserChoice) {
    case 'paper': 
      return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
    case 'rock' :
      return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
    case 'scissors' :
      return getComputerChoice === 'paper' ? 'You won' : 'The computer won!';
    default: 
      return 'Something went wrong'
  }
}
const playGam = () => {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice(3);
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}
playGam();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!