const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === "rock" ||
userInput === "paper" ||
userInput === "scissors" ||
userInput === "bomb"
) {
return userInput;
} else {
return "Error!";
}
};
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return "rock";
break;
case 1:
return "paper";
break;
case 2:
return "scissors";
}
};
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return "The game is a tie";
}
if (userChoice === "rock") {
if (computerChoice === "paper") {
return "The computer won!";
} else {
return "user won";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "computer won";
} else {
return "user won";
}
}
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "computer won";
} else {
return "user won";
}
}
if (userChoice === "bomb") {
return "detavitca , you won";
}
};
const playGame = () => {
const userChoice = getUserChoice("bomb");
const computerChoice = getComputerChoice();
console.log(
`your choice was ${userChoice} and the computer choose ${computerChoice} so = `
);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
here is my code at the end of the rock,paper,scissor project in the functions syllabus
it works fine except when the const userChoice = getUserChoice("bomb"); in the line 62 is empty like thisconst userChoice = getUserChoice(" "); the output is your choice was Error! and the computer choose paper so = undefined i tried to enhace/change the code to =
const playGame = () => {
const userChoice = getUserChoice("");
const computerChoice = getComputerChoice();
if (userChoice === "") {
return "";
} else {
console.log(
`your choice was ${userChoice} and the computer choose ${computerChoice} so = `
);
}
console.log(determineWinner(userChoice, computerChoice));
};
but i still get the same output your choice was Error! and the computer choose rock so = undefined(i just want so nothing get log on the terminal)
i tried to do it with logical operations and it ended up like this
const playGame = () => {
const userChoice = getUserChoice("");
const computerChoice = getComputerChoice();
if (userChoice !== "") {
return "";
} else {
console.log(
`your choice was ${userChoice} and the computer choose ${computerChoice} so = `
);
}
console.log(determineWinner(userChoice, computerChoice));
};
but now nothing will be shown at the terminal no matter whta i type in const userChoice = getUserChoice("//here no matter what i type");
can you please help me i even tried couple of other ways (i just dont type them here so it wont get longer than it is )
can someone please help me @mtf (i tag you specifically , because i know you can help me faster)
thanks
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === "rock" ||
userInput === "paper" ||
userInput === "scissors" ||
userInput === "bomb"
) {
return userInput;
} else {
return "Error!";
}
};
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return "rock";
break;
case 1:
return "paper";
break;
case 2:
return "scissors";
}
};
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return "The game is a tie";
}
if (userChoice === "rock") {
if (computerChoice === "paper") {
return "The computer won!";
} else {
return "user won";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "computer won";
} else {
return "user won";
}
}
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "computer won";
} else {
return "user won";
}
}
if (userChoice === "bomb") {
return "detavitca , you won";
}
};
const playGame = () => {
const userChoice = getUserChoice("");
const computerChoice = getComputerChoice();
if (userChoice === "Error!") {
return "";
} else {
console.log(
`your choice was ${userChoice} and the computer choose ${computerChoice} so = `
);
}
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
i did it at line 64 and yes it work if i dont type and of the aviable option if will show nothing just like i wanted but there is another problem at line 65 if i write anything
if (userChoice === "Error!") {
return "//anything here";
i see ,
so i think i did it fase all the time
look the thing i want is that =
1 = when there is no userChoice in the console we see a message hat says please choose
2 = if the input was wrong (something except of rock,paper,scissors) the cosole say to choose the correct one
we alread did the number 2 , becuase when the input is wrong there will be nothing in the console (or if i remove the line 64 it will say Error! but we have another problem if i dod that the output will be like this = your choice was Error! and the computer choose paper so = undefined
what should i do about it
If you haven’t solved this by tomorrow, tag me in again. Spend some time going through the logic, given what you now know and see if you can solve this on your own.
1 = when there is no userChoice (at line 62) in the console we see a message
that says : choose something
2 = if the input was wrong (something except of rock,paper,scissors) the cosole
say: choose the correct one
but also this = your choice was undefined and the computer choose scissors so = undefined will get logged to the console aswell
the problem is (if the problem is that , because i dont know what ese is the problem) line 64 until line 67 wont get execute
i’ve tried to replace userChoice at line 64 and 66 with userInput but then i get this error = ReferenceError: userInput is not defined i tried to replace them also with getUserChoice didnt work either ,
i know i must replace 64 and 66 with something but with what ? (i tried the things i could think of)
here is the code =
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === "rock" ||
userInput === "paper" ||
userInput === "scissors" ||
userInput === "bomb"
) {
return userInput;
} else if (userInput === "") {
console.log("choose something");
} else {
console.log("choose the correct one");
}
};
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return "rock";
break;
case 1:
return "paper";
break;
case 2:
return "scissors";
}
};
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return "The game is a tie";
}
if (userChoice === "rock") {
if (computerChoice === "paper") {
return "computer won";
} else {
return "user won";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "computer won";
} else {
return "user won";
}
}
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "computer won";
} else {
return "user won";
}
}
if (userChoice === "bomb") {
return "detavitca , you won";
}
};
const playGame = () => {
const userChoice = getUserChoice("");
const computerChoice = getComputerChoice();
if (userChoice === "choose something") {
console.log;
} else if (userChoice === "choose the correct one") {
console.log;
} else {
}
console.log(
`your choice was ${userChoice} and the computer choose ${computerChoice} so = `
);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
At this point my help is not what you need. You need to go back to the beginning and get your house in order. This is not a field where you can get others to do your learning for you. You have to do that yourself.
Keep trying, but give yourself a blueprint (a sketch on paper) and make your logic work. By tomorrow you may have succeeded. If not, post your entire code in formatted form (raw text in a post) not in that useless codebyte.
Posting formatted code is another thing you might have to learn. Look for the instructions in the New User section.
i didnt asked you to solve it i already solved 80% of it but i got stuck no matter what i write i get the your choice was undefined and the computer choose scissors so = undefined