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();
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';
}
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();