Rock Paper Scissors Project (without the guidelines)

Hi !

I tried to do this exercice on my own without following the guidelines. Please comment and tell me what you think about it. If you want to do the same exercice do not hesitate to add your code.

Thank you !

console.log('hi'); //get user choice const userChoice = 'paper'; console.log(`User choice is ${userChoice} `) //get computer choice let computerChoice = Math.floor((Math.random()) * 3); console.log(computerChoice); switch (computerChoice) { case 0: computerChoice = 'rock'; break; case 1: computerChoice = "scissors"; break; case 2: computerChoice = "paper"; break; default: console.log('this is not a valid move'); } console.log(`Computer choice is ${computerChoice}`) //compare the two choices and determine a winner const game = (userChoice, computerChoice) => { if (userChoice === 'rock' && computerChoice === 'scissors') { console.log(`User chose ${userChoice} and computer chose ${computerChoice}, user win`); } else if (userChoice === 'rock' && computerChoice === 'paper') { console.log(`User chose ${userChoice} and computer chose ${computerChoice}, user loose`) } else if (userChoice === 'paper' && computerChoice === 'rock') { console.log(`User chose ${userChoice} and computer chose ${computerChoice}, user wins`) } else if (userChoice === 'paper' && computerChoice === 'scissors') { console.log(`User chose ${userChoice} and computer chose ${computerChoice}, user wins`) } else { console.log('User and computer chose the same, match nul'); } } game(userChoice, computerChoice);
1 Like
  • If the user chooses "paper" and the computer chooses "scissors", does your program give the correct output?

  • If the user chooses "scissors", does your program give the correct output?

2 Likes

Hi mtrtmk,

thank you for your reply. i corrected the code.

console.log('hi'); //get user choice const userChoice = 'rock'; console.log(`User choice is ${userChoice} `) //get computer choice let computerChoice = Math.floor((Math.random()) * 3); switch (computerChoice) { case 0: computerChoice = 'rock'; break; case 1: computerChoice = "scissors"; break; case 2: computerChoice = "paper"; break; default: console.log('this is not a valid move'); } console.log(`Computer choice is ${computerChoice}`) //compare the two choices and determine a winner const game = (userChoice, computerChoice) => { if (userChoice === 'rock' && computerChoice === 'scissors') { console.log(`User chooses ${userChoice} and computer chooses ${computerChoice}, user wins`); } else if (userChoice === 'rock' && computerChoice === 'paper') { console.log(`User chooses ${userChoice} and computer chooses ${computerChoice}, user looses`) } else if (userChoice === 'paper' && computerChoice === 'rock') { console.log(`User chooses ${userChoice} and computer chooses ${computerChoice}, user wins`) } else if (userChoice === 'paper' && computerChoice === 'scissors') { console.log(`User chooses ${userChoice} and computer chooses ${computerChoice}, user looses`) } else if (userChoice === 'scissors' && computerChoice === 'rock') { console.log(`User chooses ${userChoice} and computer chooses ${computerChoice}, user looses`) } else if (userChoice === 'scissors' && computerChoice === 'paper') { console.log(`User chooses ${userChoice} and computer chooses ${computerChoice}, user wins`) } else { console.log('User and computer choose the same, tie game!'); } } game(userChoice, computerChoice);