0% found this document useful (0 votes)
58 views2 pages

ROck Paper Scissors

This document contains code for a rock-paper-scissors game that allows a user and computer to make choices. It includes functions to get the user's choice, randomly generate the computer's choice, determine the winner based on the choices, and run a game. The determineWinner function uses if/else statements to check the choices and return whether it was a tie, computer win, or user win. The playGame function calls the other functions to run a sample game.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

ROck Paper Scissors

This document contains code for a rock-paper-scissors game that allows a user and computer to make choices. It includes functions to get the user's choice, randomly generate the computer's choice, determine the winner based on the choices, and run a game. The determineWinner function uses if/else statements to check the choices and return whether it was a tie, computer win, or user win. The playGame function calls the other functions to run a sample game.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const getUserChoice = (userInput) => {

userInput = userInput.toLowerCase();

if (userInput === "rock" || "paper" || "scissors" || "bomb")


{ return userInput;
} else {
console.log("Type correct Input");
}
}

function getComputerChoice() {
let randomChoice;
randomChoice = Math.floor(Math.random()*3);
return randomChoice;

function computerdecision(){
const printComputerChoice = getComputerChoice();

if (printComputerChoice === 0) {
return "rock";
} else if (printComputerChoice === 1) {
return "paper";
} else if (printComputerChoice === 2) {
return "scissors";
} else {
return "Incorrect Input";
}
}

function determineWinner(userChoice,computerChoice) {
if (userChoice === computerChoice) {
return "Tie";
}
if ( userChoice === "rock"){
if (computerChoice === "paper"){
return "Computer Won";
} else {
return "User Won";
}
}

if (userChoice === "paper"){


if(computerChoice === "scissors") {
return "Computer Won"; }
else if(computerChoice === "rock")
{return "User Won";}
}

if (userChoice === "scissors") {


if (computerChoice === "rock") {
return "Computer Won";
} else if (computerChoice === "paper")
{ return "User Won";}
}

if(userChoice === "bomb"){


return "User Won";
}

function playGame () {
var userChoice = getUserChoice("bomb");
var computerChoice = computerdecision();

console.log("User Choice: " + userChoice);


console.log("Computer Choice: " + computerChoice);

console.log(determineWinner(userChoice, computerChoice));
}

playGame();

You might also like