Rock, Paper, or Scissors


#1

Hello everyone,

I’m happy to share my code from the test Rock, Paper, Scissors and I will be very happy to have in comment your own code to compare with mine :slight_smile:

Please I will also be very happy if the code you share with me is very differente from the classic if else solution.
My goal is simple get more knowledge from different thinking pattern to reach the same goal : a working Rock,Paper, Scissors working game :slight_smile:

Ok enough words here is my code :

const userChoice = (myChoice)=>{
	myChoice = prompt("Choisi Pierre, Feuille ou Ciseaux");
	if(myChoice === "Pierre" || "Feuille" || "Ciseaux"){
		alert(myChoice);
		return myChoice;
	}
	else{
		userChoice();
	}
const computerChoice =()=>{
	var test = Math.round(Math.random()*2+1)
	alert(test);
	var result = "";
	if(test===1){
		result = "Pierre";
	}
	else if(test===2){
		result = "Feuille";
	}
	else{
		result="Ciseaux";
	}
	alert(result);
	return result;
}
const compare = ()=>{
	var choice = userChoice();
	var choiceComputer = computerChoice();
	if(choice === choiceComputer){
		return "Egalite";
	}
	else if(choice === "Pierre" && choiceComputer ==="Feuille"){
		return "Computer wins";
	}
	else if(choice === "Feuille" && choiceComputer === "Ciseaux"){
		return "Computer wins";
	}
	else{
		return "User wins";
	}
}

Thank all :slight_smile:


#2

Your userChoice function is a bit hard to follow (also slight syntax issue, it’s missing another } bracket after the else). So you are taking in myChoice as a parameter, but then setting it to a prompted value? Then I think taking in a parameter is unnecessary there.

Also, if(myChoice === "Pierre" || "Feuille" || "Ciseaux") will not work as expected.
It isn’t comparing myChoice with the 3 strings. It’s taking the or of (myChoice === "Pierre"), ("Feuille"), and ("Ciseaux") separately. This means the if condition will always be true, and it will not re-prompt from invalid answers.


#3

Are you sure ??.

It doesn’t not seem true, cause If I prompt a value that it is not “Pierre”, “Feuille” or “Ciseaux” It prompt until the value is correct.

It seems to me that it is working

What do you think ?


#5

Sorry, I didn’t read your reply correctly. On my end, I only added an ending bracket after userChoice for the syntax error, and called userChoice() at the end of the file, since I don’t think the game runs automatically right now?

If I run that code, it prompts me for a value, and entering a random string makes it alert the string and exit, which I don’t think is expected.


#6

ahhhhhhhhhhh ok
Man you should call the compare function not the userChoice.
Cause it is nested functions


#7

I see. Ok, I called compare() at the end, and it still doesn’t reprompt if I enter a nonsense string. Are you getting the same behavior?


#8

Consider,

a = "a string"

if (a === 'string" || 'something' || 'nothing')

will always be true. Even though the first condition fails, the next one is truthy since it is a non-empty string.

if ('something')

will be true. A string expression is truthy.

if (a === 'string' || a === 'something' || a === 'nothing')

will be false for ‘a string’. Make sense? All the operands need to be boolean expressions (comparisons, in this case).