Javascript project : Rock, Paper Scissors

Hey Guys,

Anyone can explain how I must modify this code to see the result printed on the console under the choice of the user and the choice of the computer? I tried to figured out but it looks not so easy for noobs like me. I simply followed the tasks assigned from the project but it looks like something is missing.

Thank you in advance for you help :slight_smile:
Here’s the code :

//User choice

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput == “rock” || userInput == “paper” || userInput == “scissors” || userInput == “bomb”) {
return userInput;
} else {
console.log(“Enter a valid choice!”);
}
};

//Computer choice

function getComputerChoice() {
let choice = Math.floor(Math.random() * 3);
switch (choice) {
case 0:
return “paper”;
case 1:
return “scissors”;
case 2:
return “rock”;
}
}

//Determining winner

function determineWinner(userChoice, computerChoice) {
if (userChoice == computerChoice) {
return “No winners”;
}
if (userChoice == “rock”) {
if (computerChoice == “paper”) {
return “Computer won!”;
} else {
return “You won!”;
}
}
if (userChoice == “paper”) {
if (computerChoice == “scissors”) {
return “Computer won!”;
} else {
return “You Won!”;
}
}
if (userChoice == “scissors”) {
if (computerChoice == “rock”) {
return “Computer won!”;
} else {
return “You won!”;
}
}
if (userChoice == ‘bomb’) {
return “You won!”;
}
}

// Play game

const playGame = () => {
const userChoice = getUserChoice(“bomb”);
const computerChoice = getComputerChoice();
console.log("You threw: " + userChoice);
console.log("The computer threw: " + computerChoice);
};
playGame();

You could put
console.log(determineWinner(userChoice, computerChoice));
(to call the determineWinner function and display the what gets returned as a result of that call)
at the end of the playGame function
so that the winner is printed to the console.

//User choice const choice = "bomb"; const getUserChoice = (userInput) => { userInput = userInput.toLowerCase(); if (userInput == "rock" || userInput == "paper" || userInput == "scissors" || userInput == "bomb") { return userInput; } else { console.log("Enter a valid choice!"); } }; //Computer choice function getComputerChoice() { let choice = Math.floor(Math.random() * 3); switch (choice) { case 0: return "paper"; case 1: return "scissors"; case 2: return "rock"; } } //Determining winner function determineWinner(userChoice, computerChoice) { if (userChoice == computerChoice) { return "No winners"; } if (userChoice == "rock") { if (computerChoice == "paper") { return "Computer won!"; } else { return "You won!"; } } if (userChoice == "paper") { if (computerChoice == "scissors") { return "Computer won!"; } else { return "You Won!"; } } if (userChoice == "scissors") { if (computerChoice == "rock") { return "Computer won!"; } else { return "You won!"; } } if (userChoice == "bomb") { return "You won!"; } } // Play game const playGame = () => { const userChoice = getUserChoice(choice); const computerChoice = getComputerChoice(); console.log("You threw: " + userChoice); console.log("The computer threw: " + computerChoice); const winner = determineWinner(userChoice, computerChoice); console.log(winner); }; playGame();
1 Like

Thanks m8! Now I got it…was not so difficult finally…I’m still having problems in the understanding of JavaScript codes…