Invalid message

I am currently new to JavaScript and am in school! There is a task that I do when creating a game, but the warning continues to appear with the wrong one. Every time he warns that "you have found a match!" for each card, however this should not happen. I tried to figure it out in the last hour. Thanks

var cards = ["queen", "king", "queen", "king"];

var cardsInPlay = [];

var cardOne = cards[0];

cardsInPlay.push(cardOne);

console.log("User flipped " + cardOne);

var cardTwo = cards[1];

cardsInPlay.push(cardTwo);

console.log("User flipped " + cardTwo);

if (cardsInPlay.length === 2){
    cardsInPlay[0] === cardsInPlay[1];
    alert("You found a match!");
} else {
        alert("Sorry, try again");
    }
+4
source share
2 answers

I think you should put a condition as shown below:

if (cardsInPlay.length === 2 && cardsInPlay[0] === cardsInPlay[1]) {
  alert("You found a match!");
}
0
source

You have a simple syntax error.

if (cardsInPlay.length === 2){
  cardsInPlay[0] === cardsInPlay[1];

By placing the second condition inside the bracket {, you made it ineffective. Try the following:

if (cardsInPly.length === 2 && cardsInPlay[0] === cardsInPlay[1]) {

( ). , .

cardsInPlay[0] === cardsInPlay[1];, , false;. , .

+2

Source: https://habr.com/ru/post/1671089/


All Articles