Tsa*_*nes 0 javascript arrays console function
(免责声明:我对JS很新)
嗨,大家好,
我正在开发一款随机挑选两张牌的游戏,根据结果你会得到一个弹出窗口.我一直在按照一套说明来完成它.
我有两个问题:
A.在控制台中我收到一条错误消息:
SyntaxError:missing; 在声明之前
它指的是{else 7号线上的},我不知道为什么我会得到它.假设我错过了一个;它在哪里?我想我会问另一组眼睛.
B.说明说我应该在控制台看到"用户翻转女王"和"用户翻王",但我不是.
如果问题1中的问题得到解决,我会看到它们吗?如果没有,那我该怎么办?
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);
Run Code Online (Sandbox Code Playgroud)答:您错过了区块{
的开头和if
区块}
的末尾else
.
var checkForMatch = function() {
if (cardsInPlay[0] === cardsInPlay[1]) { // <=== here
alert('You found a match!');
} else {
alert('Sorry, try again.');
}
}
Run Code Online (Sandbox Code Playgroud)
B.由于代码中存在语法错误,因此无法正常运行.
你也错过}
了flipCard
函数的结尾.
Javascript区分大小写.由于您命名了该函数flipCard
,因此无法将其命名为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);
Run Code Online (Sandbox Code Playgroud)