Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getUserChoice = userInput => {
- userInput = userInput.toLowerCase();
- if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
- return userInput;
- }
- else {
- console.log('error, not a valid choice. Please choose between "rock, paper, or scissors"');
- }
- };
- const getComputerChoice = computerInput => {
- Math.floor(Math.random() * 3);
- if (computerInput === 0) {
- return 'rock';
- }
- if (computerInput === 1) {
- return 'paper';
- }
- if (computerInput === 2) {
- return 'scissors';
- }
- };
- const determineWinner = (userChoice, computerChoice) => {
- if (userChoice === computerChoice) {
- return 'The game is a tie!';
- }
- if (userChoice === 'scissors') {
- if (computerChoice === 'paper') {
- return 'You have won!.';
- }
- else {
- return 'The computer has won.';
- }
- }
- if (userChoice === 'paper') {
- if (computerChoice === 'rock') {
- return 'You have won!.';
- }
- else {
- return 'The computer has won.';
- }
- }
- if (userChoice === 'rock') {
- if (computerChoice === 'scissors') {
- return 'You have won!.';
- }
- else {
- return 'The computer has won.';
- }
- }
- if (userChoice === 'bomb') {
- if (computerChoice === 'rock') {
- return 'Cheater....';
- }
- }
- if (userChoice === 'bomb') {
- if (computerChoice === 'scissors') {
- return 'Cheater....';
- }
- }
- if (userChoice === 'bomb') {
- if (computerChoice === 'paper') {
- return 'Cheater....';
- }
- }
- };
- const playGame = () => {
- const userChoice = prompt();
- const computerChoice = prompt();
- console.log('The user chose: ' + userChoice);
- console.log('The computer chose: ' + computerChoice);
- console.log(determineWinner(userChoice, computerChoice));
- };
- playGame();
Add Comment
Please, Sign In to add comment