"Rock, Paper, Scissors" The next step almost complete, but


#1


I already did the loop for null and incorrect input, but I still can't do the draw loop, can anyone tell me which part of my code is wrong? Thank you.


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


var choiceData=["rock","paper","scissors"];
var userChoice = prompt("Do you choose rock, paper or scissors?");{
	while (userChoice===null||userChoice!=choiceData.find(n=>n==userChoice)){
	    alert("Error! Please try again!");
	    var userChoice = prompt("Do you choose rock, paper or scissors?");
	}
}
var computerChoice = Math.random();{
	if (computerChoice < 0.34) {
		computerChoice = "rock";
	}else if(computerChoice <= 0.67) {
		computerChoice = "paper";
	}else {
		computerChoice = "scissors";
	}
}
var response=("You choose "+userChoice+" and Computer choose "+computerChoice+". ");
function compare(choice1,choice2){
	if (choice1==="rock") {
		if (choice2==="scissors") alert("Congratulations! "+response+"Your rocks wins!");
		else alert("Oh no! "+response+" Computer's paper wins!");
	}else if (choice1==="paper"){
		if (choice2==="rock") alert("Congratulations! "+response+"Your paper wins!");
		else alert("Oh no! "+response+" Computer's scissors wins!");
	}else {
		if (choice2==="paper") alert("Congratulations! "+response+"Your scissors wins!");
		else alert("Oh no! "+response+"Computer's rock wins!");
	}
}
while(userChoice===computerChoice){alert(response+"Draw! Try again!");		//Where should I put this...
		var userChoice = prompt("Do you choose rock, paper or scissors?");
}
compare(userChoice,computerChoice);


#2

alert("Error! Please try again!");

An infinite loop that nullifies the Cancel button and the Escape key. Considering we are using prompt() this is an unfortunate circumstance.

Hint: Disallow this prompt any further, then save and refresh the page after correcting the code.


#3

The general wherewithal of this question exceeds the current level of code awareness. That brings into question the genuineness of the question. We cannot comment on copied code. Explain or abandon, at this point.


#4

Thanks for the answer. I had changed this code

userChoice!=choiceData.find(n=>n==userChoice)

into

var choiceData=["rock"||"paper"||"scissors"];
var userChoice = prompt("Do you choose rock, paper or scissors?");{
while (userChoice===null||userChoice!=choiceData){
alert("Error! Please try again!");
var userChoice = prompt("Do you choose rock, paper or scissors?");
}
}

I purposely keep the loop for cancel so that nobody can escape if they don't try to answer. The main problem here is not about null and choiceData loop, it's about:

while(userChoice===computerChoice){alert(response+"Draw! Try again!"); //Where should I put this...
var userChoice = prompt("Do you choose rock, paper or scissors?");
}

I want to create loop that alert the user to try again and prompt again if the userChoice===computerChoice. But I am not sure where should I put this block and it keep continue to create infinite loop. Help me please, thank you.


#5

The code is not what is taught in the module so I'll be moving this topic to Corner Bar for open discussion away from the lesson.


#6

It is never a good practice to disable a user's ability to exit at any time. Lock them in a loop goes against the intent of Cancel/Esc.

What is evident here is that the OP has still not mastered some of the necessary skills needed to take this project to the next level. I would recommend completing the entire JS track then come back to this project when you will be better equipped.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.