How to break from Jquery click callback function

I am making my own version of simon Game, but the callback function inside click is not existing after user press wrong input. As a result function handler.patternRepeatPlayer() is getting called recursivelly for each element of array pattern.
I want a solution to break from the else after calling handler.patternRepeatPlayer just once.
The program is working fine in strict mode, but only in non-strict mode inside else , I am not
able to break from else.

The flow is like from html -> Function simonnGame.PatternGen from PatternGen -> handler.PatternRepeatPlayer -> PatternRepPlayer -> PatternMatcher -> userInput(here if wrong user input in non-strict mode) -> patternRepeatPlayer This case is failing as in this case else is not existing after calling the function only once.
can access the project on Git -

I think there is some misunderstanding on how click callback functions work.


var simonGame = {
	COUNT: 0,
	patternGen: function(){
		var randomId;
		randomId = Math.floor(Math.random() * 7);
		if(simonGame.COUNT > 20){
			alert("You have won the game!!");
		simonGame.COUNT += 1;
		//console.log("increase count true calling count display " + simonGame.COUNT);
		//console.log("count gen true calling patternPlayer with PATTERN " + simonGame.PATTERN );
	}, //close patternGen
	patternMatcher: function(genPattern){
		//console.log("inside patternMatch");
		var genPattern = simonGame.patternGen;
			//console.log("PATEERN: " + simonGame.PATTERN + "COUNT " + simonGame.COUNT );
			//calling user input
			console.log("calling user Input");
				if(handler.repeatFlag === false){  //execute count gen only if repeat flag is false inside user INPUT
			//console.log("pattern check true, calling pattern gen");
		//},simonGame.COUNT*5000); //close setTimeout
	}, //close patternMatcher
} //close simonGame

var handler = {
	countRepPlayer: 0,
	repeatFlag: false,
	patternRepeatPlayer: function(){
		var repeater = setInterval(function(){
				handler.countRepPlayer += 1;
				if(handler.countRepPlayer > simonGame.COUNT){
					handler.countRepPlayer = 0;
			},1000);//close sestInterval
	}, //close patternRepeatPlayer
	effect: function(id){
	   var img = document.getElementById(id);
	   if(img !== null && id !== undefined){
			$( img ).fadeIn(100).fadeOut(100).fadeIn(100);//fadeOut(200).fadeIn(200);
			var audio = new Audio(simonGame.SOUND[id].file);;
			//console.log("id inside effect " + id)
	},//close effect
	countDisplay: function(){
		document.getElementById("count").innerHTML = "Count: " + simonGame.COUNT;
	}, //close countIncrease
	userInput: function(){
		var userPattern = new Array();var id;
				id = parseInt(,10);
				console.log(" user " + userPattern); 
				console.log(" pattern " + simonGame.PATTERN);
				if(userPattern.indexOf(id) !== simonGame.PATTERN.indexOf(id)){
					console.log(" WRONG USER INPUT ");
					if($('.chkStrict:checked').val() === "on"){
						var audio = new Audio('sounds/wrong.mp3');;
					} else {
						console.log("inside else " );
						var audio = new Audio('sounds/wrong.mp3');;
						userPattern.length = 0;
						handler.repeatFlag = true;
						handler.patternRepeatPlayer(); //this is getting called recursivelly rather than quiting after calling once.
						return ;
				//reset the userPattern Array
				if(userPattern.length === simonGame.PATTERN.length){
					userPattern.length = 0;
		});		//close click.
} //close handler
1 Like

Please be sure to surround code samples with a line of three backticks (above the ESC key) and follow your code with the same line of three backticks. I’ve added it to your post to make it readable.

1 Like

Do you have this in a codeped or something where we can see what’s happening? One of the reasons FCC recommends that it makes it sooooo much easier for people to help you with problems.

When I download this to my computer and run it, the first thing i notice is that it’s telling me I’m wrong, even when I give the right answer. It seems to log the click correctly but your logic calls it “wrong”. I’d look at that first.

It seems to me (on a quick look, gotta go for my run before it gets too hot) that the click is firing multiple times. Have you tried something like:


This will keep the click from accidentally firing again. Now, when I run it, and I hit a wrong answer (every answer is wrong, as I mentioned), it plays it twice, presumably one for my press and once for the replay (though you might want a delay in there.) This is an improvement from when it kept building up calls and calling it over and over.

Just as a point definition, I don’t think we’re talking about recursion here, where a function calls itself.

I have shared the url of git repo of this project. Its capturing the user input correctly, only thing is there is delay when the function to check user input after pattern repeat may be because of that you have come across a case where right input is also coming wrong.

But I have not come across such case though I can try immediate propagation and see what happens.

main issue I think can be with setInterval patternRepater which is making multiple calls to patterRepeatPlayer and then to function userInput for one wrong input. that is why the program in non strict mode is not working.

Thanks for taking time to check my problem and suggest solution.

I won’t debug this, but you can “break” from any function with a return statement. You should probably find some way to stop the recursive calls, though.

<p> I have solved this problem by moving away from jquery , and simply 
   setting event handler on divs by wrapping each image inside one div using 
   java script. Below is the sample code. 


                var img = document.getElementById(div);
                img.onclick = function(){
                    var id = parseInt(,10);
                    console.log(" div clicked " + id + " u p " + userPattern);
                    if(userPattern.indexOf(id) !== simonGame.PATTERN.indexOf(id)){
                        console.log(" if ");
                    } else if(userPattern.length === simonGame.PATTERN.length){

        }); console.log(" up " + userPattern + " SGP " + simonGame.PATTERN);</p>