In the code below I keep getting undefined values. Can someone explain why this is happening?
In this code I utilize math.random
to generate a number between 0-0.99. Than I multiply it by three. After that I floor
it so that I always get a 0, 1, 2. And than its supposed to generate a random number that I use for my switch case.
const userChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === "rock" ||
userInput === "scissors" ||
userInput === "paper"
) {
return userInput;
} else {
console.log("Error, please type: rock, paper or scissors.");
}
};
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
}
};
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return "This game is a tie!";
}
};
if (userChoice === "rock") {
if (computerChoice === "paper") {
return "sorry, computer won!";
} else {
return "Congratulations, you won!";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "Sorry, computer won!";
} else {
return "Congratulations, you won!";
}
}
if (userChoice === "scissors") {
if (computerChoice === "paper") {
return "Sorry, computer won!";
} else {
return "Congratulations, you won!";
}
}
console.log(determineWinner("rock", "scissors"));
console.log(determineWinner("paper", "scissors"));
// task 7
// when the text is not colored normally that is a sign of what is wrong.
// if you see a red ending bracket it is most likely an extra one you do not need.
// you do not need to have parenthesis around the return
// task 8
// we already covered what happens in a tie in the last task. in this task we say if the player chooses rock, than there are only two other scenarios. one in which the computer picks paper, in which case, the comptuer wins. and the ohter where the computer picks scissors, and the player wins. the else statement is for scissors and doesn't need to be fully written out becuase it is the only option left.
I have tried changing the case values from 0,1,2 to 1,2,3. I have tried changing the input for the determine winner function to getComputerChoice
which in retrospect seems kind of dumb of me because I need the switch case to work.
I need to understand why its saying undefined in the first place.
You're super close to the solution in your example. The issue is being caused by the if statements not being contained within the determineWinner() function, so you'll just need to move the closing bracket for determineWinner to include the extra if statement. Here's what that looks like:
const userChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === "rock" ||
userInput === "scissors" ||
userInput === "paper"
) {
return userInput;
} else {
console.log("Error, please type: rock, paper or scissors.");
}
};
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
}
};
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return "This game is a tie!";
}
if (userChoice === "rock") {
if (computerChoice === "paper") {
return "sorry, computer won!";
} else {
return "Congratulations, you won!";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "Sorry, computer won!";
} else {
return "Congratulations, you won!";
}
}
if (userChoice === "scissors") {
if (computerChoice === "paper") {
return "Sorry, computer won!";
} else {
return "Congratulations, you won!";
}
}
};
console.log(determineWinner("rock", "scissors"));
console.log(determineWinner("paper", "scissors"));
// task 7
// when the text is not colored normally that is a sign of what is wrong.
// if you see a red ending bracket it is most likely an extra one you do not need.
// you do not need to have parenthesis around the return
// task 8
// we already covered what happens in a tie in the last task. in this task we say if the player chooses rock, than there are only two other scenarios. one in which the computer picks paper, in which case, the comptuer wins. and the ohter where the computer picks scissors, and the player wins. the else statement is for scissors and doesn't need to be fully written out becuase it is the only option left.