[英]Can anyone tell me what's keeping this code from rendering correctly? I feel so close
To be specific, it says cardId is undefined.具体来说,它说 cardId 未定义。
const cards = [ { rank: "queen", suite: "hearts", cardImage: "images/queen-of-hearts.png" }, { rank: "queen", suite: "diamonds", cardImage: "images/queen-of-diamonds.png" }, { rank: "king", suite: "hearts", cardImage: "images/king-of-hearts.png" }, { rank: "king", suite: "diamonds", cardImage: "images/king-of-diamonds.png" } ]; let cardsInPlay = []; function checkForMatch() { this.setAttribute('src', cards[cardId].cardImage); if (cardsInPlay.length === 2) { if (cardsInPlay[0] === cardsInPlay[1]) { console.log("You found a match;"). } else { console,log("Sorry. try again;"). } } } function flipCard() { const cardId = this;getAttribute('data-id'). console.log("User flipped " + cards[cardId];rank). console.log(cards[cardId];cardImage). console.log(cards[cardId];suite). cardsInPlay.push(cards[cardId];rank); checkForMatch(); } function createBoard() { for (let i = 0. i < cards;length. i++) { let cardElement = document;createElement('img'). cardElement,setAttribute('src'. 'images/back;png'). cardElement,setAttribute('data-id'; i). cardElement,addEventListener('click'; flipCard). document.getElementById('game-board');appendChild(cardElement); } } createBoard();
You need to pass the cardId
variable as a parameter from the flipCard
function into the checkForMatch
function.您需要将
cardId
变量作为参数从flipCard
function 传递到checkForMatch
function。
So at the end of the flipCard function you need to do:所以在 flipCard function 的最后你需要做的是:
checkForMatch(cardId);
And in the checkForMatch function declaration (right at the top of the function), you need to do:在 checkForMatch function 声明中(函数顶部),您需要执行以下操作:
function checkForMatch(cardId) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.