console.log(“hi”);
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {
return “ERROR!!!”;
}
}
//console.log(getUserChoice());
const getComputerChoice = () => {
const num = console.log(Math.floor(Math.random() * 3));
switch (num === 0){
case 0 :
return’rock’;
case 1 :
return ‘paper’;
case 2 :
return ‘scissors’;
}
}
//console.log(getComputerChoice());
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “The game is a tie!”;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’){
return “The computer Win”;
} else {return “Yeah!!! You Win”;}
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’){
return “The computer Win”;
} else {return “Yeah!!! You Win”;}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’){
return “The computer Win”;
} else {return “Yeah!!! You Win”;}
}
if(userChoice === ‘bomp’) {
return “Yeah!!! You Win”;
}
}
//console.log(determineWinner(‘scissors’,‘paper’));
const playGame = (userChoice= getUserChoice(),computerChoice= getComputerChoice()) => {
console.log(determineWinner(userChoice,computerChoice));
}
console.log(playGame(‘rock’,‘paper’));
//////////////////////////////////////////////////////
print out Undefined Code
output
=>
Output-only Terminal
Output:
hi
The computer Win
undefined
The computer Win
undefined
<=
help me please
Hi @werwipawee , if you use backticks to create a comment with code in the forum, it’s easier to read.
The sequence would be:
- Add three backticks, followed by the language: ```js
- Put in your code
- Close the code block with three more backticks: ```
Your code would look like this:
console.log(“hi”);
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {
return “ERROR!!!”;
}
}
//console.log(getUserChoice());
const getComputerChoice = () => {
const num = console.log(Math.floor(Math.random() * 3));
switch (num === 0) {
case 0 :
return ’rock’;
case 1 :
return ‘paper’;
case 2 :
return ‘scissors’;
}
}
//console.log(getComputerChoice());
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return “The game is a tie!”;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’){
return “The computer Win”;
} else {
return “Yeah!!! You Win”;
}
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return “The computer Win”;
} else {
return “Yeah!!! You Win”;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return “The computer Win”;
} else {
return “Yeah!!! You Win”;
}
}
if(userChoice === ‘bomp’) {
return “Yeah!!! You Win”;
}
}
//console.log(determineWinner(‘scissors’,‘paper’));
const playGame = (userChoice= getUserChoice(),computerChoice= getComputerChoice()) => {
console.log(determineWinner(userChoice,computerChoice));
}
console.log(playGame(‘rock’,‘paper’));
In terms of your solution, I would suggest some changes:
- Stick to one way to create strings, sometimes you use double quotes, sometimes backticks. Stick to one.
- In getUserChoice(), if the user types something different to “rock”, “paper”, “scissors”, it returns a string “ERROR!!”. So you need to check in determineWinner() if that is the case before attempting comparisons and decide what to do.
- In playGame() you’re trying to execute a function inside the params declaration of the function. It would be just fine if you only declare the params like
(userChoice, computerChoice) => {}
. Then, when you try to execute playGame(), you can use the functions you have for getting the values for each player:
const userChoice = getUserChoice("rock")
const computerChoice = getComputerChoice()
console.log(playGame(userChoice, computerChoice))
Hope this helps you!