0

(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);
    
Barmar
  • 741,623
  • 53
  • 500
  • 612
Tsardines
  • 883
  • 2
  • 8
  • 17

3 Answers3

5

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

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);
sumeet kumar
  • 2,628
  • 1
  • 16
  • 24
  • Please don't encourage leaving out the braces. See https://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar Jan 16 '18 at 21:26
  • - Not sure how I missed the capital C in "flipcard"...and thanks for updating the code! – Tsardines Jan 16 '18 at 21:30
1

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.');      
}
Vincent Beltman
  • 2,064
  • 13
  • 27