Working on a JS assignment (functions, arrays, etc)---have a couple of questions

(Disclaimer: I'm very new to JS)

Hi everyone,

I'm working on a game where you pick two cards randomly, and depending on the result you'll get a pop-up. I've been following a set of instructions to complete it.

I have two questions:

A. In the console I'm getting an error message:

SyntaxError: missing ; before statement

It's referring to the } else { on Line 7, and I don't know why I'm getting it. Assuming I am missing a ;, where does it go? I figured I'd ask and get another set of eyes on this one.

B. The instructions say I'm supposed to see "User flipped queen" and "User flipped king" in the console, but I'm not.

If the issue in Question 1 is sorted out, will I see them? If not, then what do I do?

  • Thanks!
  • Tsardines

    var cards = ["queen", "queen", "king", "king"];
    var cardsInPlay = [];
    
    var checkForMatch = function() {
        if (cardsInPlay[0] === cardsInPlay[1]) 
            alert('You found a match!');
        } else {
            alert('Sorry, try again.');      
    }
    
    var flipCard = function(cardId) {
        console.log("User flipped " + cards[cardId]);
        cardsInPlay.push(cards[0]);
        if (cardsInPlay.length === 2) {
            checkForMatch();
        }
    
        flipcard(0);
        flipcard(2);
    

3 answers

  • answered 2018-01-16 21:21 Vincent Beltman

    You're missing a { after if (cardsInPlay[0] === cardsInPlay[1])

    E.g.:

    var checkForMatch = function() {
        if (cardsInPlay[0] === cardsInPlay[1]) {
            alert('You found a match!');
        } else {
            alert('Sorry, try again.');      
    }
    

  • answered 2018-01-16 21:21 sumeet kumar

    Because you have issues in your code.. your if and else blocks were not complete.

    JavaScript is case sensitive so flipCard is not flipcard.

    var cards = ["queen", "queen", "king", "king"];
    var cardsInPlay = [];
    
    var checkForMatch = function() {
        if (cardsInPlay[0] === cardsInPlay[1]){ 
                alert('You found a match!');
              }
    
             else {
                  alert('Sorry, try again.');      
                }
    }
    
    var flipCard = function(cardId) {
        console.log("User flipped " + cards[cardId]);
        cardsInPlay.push(cards[0]);
    if (cardsInPlay.length === 2) {
    
    checkForMatch();
    }
    }
    
    flipCard(0);
    flipCard(2);

  • answered 2018-01-16 21:21 Barmar

    A. You're missing the { at the beginning of the if block and the } at the end of the else block.

    var checkForMatch = function() {
        if (cardsInPlay[0] === cardsInPlay[1]) { // <=== here
            alert('You found a match!');
        } else {
            alert('Sorry, try again.');
        }   
    }
    

    B. Since you have a syntax error in your code, nothing runs correctly.

    You're also missing the } at the end of the flipCard function.

    And Javascript is case-sensitive. Since you named the function flipCard, you can't call it as flipcard.

    var cards = ["queen", "queen", "king", "king"];
    var cardsInPlay = [];
    
    var checkForMatch = function() {
        if (cardsInPlay[0] === cardsInPlay[1]) { // <=== here
            console.log('You found a match!');
        } else {
            console.log('Sorry, try again.');
        }   
    }
    var flipCard = function(cardId) {
      console.log("User flipped " + cards[cardId]);
      cardsInPlay.push(cards[0]);
      if (cardsInPlay.length === 2) {
        checkForMatch();
      }
    }
    
    flipCard(0);
    flipCard(2);