My name is Andrew and I am a rookie programmer. In my code below I keep getting undefined values in my code. Can someone explain why that is? 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. So I believe anyway. 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 chanigng 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 am stuck. I need to understand why its saying undefined in the first place.
CodePudding user response:
Only the first if
statement is inside determineWinner
, the rest is outside and have no functionality in your code. Try rearranging the brackets
CodePudding user response:
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.