0

I was wondering why my program crashes after its made its first match....any ideas would be greatly appreciated. Below is the code snippet. Thanks for the input!

var clicks = 0; //counts how may picks have been made in each turn
var firstchoice; //stores index of first card selected
var secondchoice; //stores index of second card selected
var match = 0; //counts matches made
var backcard = "deck.jpg"; //shows back of card when turned over

var faces = []; //array to store card images
faces[0] = 'pic1.jpg';
faces[1] = 'pic2.jpg';
faces[2] = 'pic3.jpg';
faces[3] = 'pic3.jpg';
faces[4] = 'pic2.jpg';
faces[5] = 'pic1.jpg';

function choose(card) {
        if (clicks === 2) {
            return;
        }
        if (clicks === 0) {
            firstchoice = card;
            document.images[card].src = faces[card];
            clicks = 1;
        } else {
            clicks = 2;
            secondchoice = card;
            document.images[card].src = faces[card];
            timer = setInterval("check()", 1000);
        }
    }
    /* Check to see if a match is made */

function check() {
    clearInterval(timer); //stop timer
    if (faces[secondchoice] === faces[firstchoice]) {
        match++;
        document.getElementById("matches").innerHTML = match;
    } else {
        document.images[firstchoice].src = backcard;
        document.images[secondchoice].src = backcard;
        clicks = 0;
        return;
    }
}
3
  • 1
    Can you explain what this code should be doing? Commented Feb 17, 2017 at 20:58
  • firstchoice and secondchoice do not has default value. try with default value and let me know th eresult Commented Feb 17, 2017 at 21:02
  • It is a javascript code that checks to see if 2 clicked images match. If they match the cards will stay face up, and if they dont they will return to face down position.... I have also tried to set both default value to 0 and the program still does not respond after the first match Commented Feb 17, 2017 at 21:07

2 Answers 2

1

The first parameter of setInterval needs to be a function not a string pretending to be a function. So you would want this:

timer = setInterval(function() { check(); }, 1000);

Of course, you can simplify:

timer = setInterval(check, 1000);

Not sure why you're using setInterval() here. You could more easily just do:

timer = setTimeout(check, 1000);

The advantage is there is no interval to clear in the check() function.

The other issue is that you are not resetting your 'clicks' counter to 0 when there is a match.

You want this:

function check() {
    clearInterval(timer); //stop timer
    if (faces[secondchoice] === faces[firstchoice]) {
        match++;
        document.getElementById("matches").innerHTML = match;
    } else {
        document.images[firstchoice].src = backcard;
        document.images[secondchoice].src = backcard;
    }
    clicks = 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone for your help, big shoutout to Rasmeister for debugging this problem!
0

I think you have to declare you timer function globally. Its only defined in the scope of the first function, so in the second when you try to clear it nothing happens:

var timer = ''; //Declare timer up here first!

function choose(card) { ... }

function check() { ... }

1 Comment

var timer=''; var firstchoice=0; //stores index of first card selected var secondchoice=0; //stores index of second card selected I have now added default value and declared timer variable but still the same issue. Thanks for the comments by the way guys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.