So I’m a little stuck here and would LOVE some help!
I’m a little confused, because when I run the code, it will return my choice, the computers choice, but will always return the “its a tie” result regardless of the choices,
and if i comment this section out and run the code again, then I get an error message after the two choices have been returned, as if the rest of the IF statements are wrong.
I have used all the hints and previous forum posts but am still none the wiser!
TIA x
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log("This isn't a gun fight! choose rock, paper or scissors!")
}
}; /*function close */
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3 );
if (randomNumber === 0) {
return 'rock';
} else if (randomNumber === 1) {
return 'paper';
} else {
return 'scissors';
}
}; /* function close */
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'we have a tie!';
};
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'the machines win!';
} else {
return "victory for sapiens!";
}
}; /* if statement close */
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return('Computer wins!')
} else {
return "Player 1,000,000 wins!";
}
}; /* if statement close */
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return "The computers are taking over... light the beacons!!";
} else {
return "We did it!";
}
}; /* if statement close */
}; /* function closes here */
console.log(getUserChoice('rock'));
console.log(getComputerChoice());
console.log(determineWinner());
Okay all done now!
I probably used too many hints to actually get anything out of that project,
But thanks for helping me to see why those variables were returning as undefined!
so, after finishing the challenge, I wanted to build on the cheat section, and add in another rule for what will happen if the computer threw a bomb, and also what would happen if we both throw bombs.
I followed the rules as closely as possible and as you can see from the code, altered the Math.random function to multiply by four to account for the extra option,
but now the output is that the computer will always throw a bomb, despite testing it several times.
to trouble shoot, I have tried moving the blocks of IF statements to different lines so they are interpreted either earlier or later, but its not working.
could anyone offer any advice here?
TIA
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('error! Play rock, paper or scissors');
};
};
const getComputerChoice = () => {
Math.floor(Math.random() * 4);
if (getComputerChoice === 0) {
return 'rock';
} else if (getComputerChoice === 1) {
return 'scissors';
} else if (getComputerChoice === 2) {
return 'paper';
} else {
return 'bomb';
} /* if/else statement close */
}; /*function close */
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === 'bomb' && computerChoice === 'bomb') {
return 'you both threw bombs.... everyone is crispy dead'
} /* if close */
if (userChoice === 'bomb') {
return 'you threw the bomb and blew up the world!'
} /* if close */
if (computerChoice === 'bomb') {
return 'the machines have bombs.... we all dead now';
}
if (userChoice === computerChoice) {
return 'tie';
} /*if statement close */
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'computer wins';
} else {
return 'player wins';
} /* if/else close */
} /* if close */
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'computer wins'
} else {
return 'player wins'
} /* if/else close */
} /* if close */
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'computer wins'
} else {
return 'player wins;'
} /* if/else close */
} /* if close */
} /*function close */
const playGame = () => {
const userChoice = getUserChoice('rock');
const computerChoice = getComputerChoice();
console.log(`you threw ${userChoice}`);
console.log(`computer threw ${computerChoice}`);
console.log(determineWinner(userChoice, computerChoice));
} /* function close */
playGame();