免责声明:我是JS的新手
您好,
我以纸牌游戏的形式从事作业。查看说明,我正确地在控制台中获取以下内容:
User flipped queen
images/queen-of-hearts.png
hearts
User flipped king
images/king-of-hearts.png
hearts
说明中还说加载html文件应显示警告,"抱歉,再试一次。"
然而,我收到的警报是"你找到了匹配!"即使两张选择的卡片没有。
我认为它可能显而易见,但我不确定它可能是什么。
以下是代码:
console.log('up and running');
var cards = [
{
rank: 'queen',
suit: 'hearts',
cardImage: 'images/queen-of-hearts.png',
},
{
rank: 'queen',
suit: 'diamonds',
cardImage: 'images/queen-of-diamonds.png',
},
{
rank: 'king',
suit: 'hearts',
cardImage: 'images/king-of-hearts.png',
},
{
rank: 'king',
suit: 'diamonds',
cardImage: 'images/king-of-diamonds.png',
}
];
var cardsInPlay = [];
var checkForMatch = function() {
if (cardsInPlay.length === 2)
checkForMatch();
if (cardsInPlay[0] === cardsInPlay[1]) {
alert('You found a match!');
} else {
alert('Sorry, try again.');
}
};
checkForMatch();
var flipCard = function(cardId) {
console.log("User flipped " + cards[cardId].rank);
console.log(cards[cardId].cardImage);
console.log(cards[cardId].suit);
cardsInPlay.push(cards[cardId].rank);
}
flipCard(0);
flipCard(2);
答案 0 :(得分:0)
我认为我看到了问题(以及潜在的问题)。我已经为您的代码添加了一些注释,我建议您按顺序阅读它们(请参阅每条评论中的数字)。
3
请注意,如果您在checkForMatch()
添加缺少的花括号(以及另一个将其关闭),您将会有无限循环!因为{
"query": {
"bool": {
"must": {
"match": {
"name": {
"query": searchString,
"fuzziness": 2,
"operator": "or"
}
}
},
"should": [
{ "term": { "category": "hospital" } },
{ "term": { "category": "disease" } }
],
"minimum_should_match": 2
}
}
}
无限地称呼自己。
答案 1 :(得分:0)
问题是双重的:
[TestClass]
public class DocumentDBRepositoryShould {
/// <summary>
/// Fake IOrderedQueryable IDocumentQuery for mocking purposes
/// </summary>
public interface IFakeDocumentQuery<T> : IDocumentQuery<T>, IOrderedQueryable<T> {
}
[TestMethod]
public async Task ExecuteQueryAsync() {
//Arrange
var description = "BBB";
var expected = new List<MyDocumentClass> {
new MyDocumentClass{ Description = description },
new MyDocumentClass{ Description = "ZZZ" },
new MyDocumentClass{ Description = "AAA" },
new MyDocumentClass{ Description = "CCC" },
};
var response = new FeedResponse<MyDocumentClass>(expected);
var mockDocumentQuery = new Mock<IFakeDocumentQuery<MyDocumentClass>>();
mockDocumentQuery
.SetupSequence(_ => _.HasMoreResults)
.Returns(true)
.Returns(false);
mockDocumentQuery
.Setup(_ => _.ExecuteNextAsync<MyDocumentClass>(It.IsAny<CancellationToken>()))
.ReturnsAsync(response);
var client = new Mock<IDocumentClient>();
client
.Setup(_ => _.CreateDocumentQuery<MyDocumentClass>(It.IsAny<Uri>(), It.IsAny<FeedOptions>()))
.Returns(mockDocumentQuery.Object);
var cosmosDatabase = string.Empty;
var documentsRepository = new DocumentDBRepository<MyDocumentClass>(cosmosDatabase, client.Object);
//Act
var query = documentsRepository.GetQueryable(); //Simple query.
var actual = await documentsRepository.ExecuteQueryAsync(query);
//Assert
actual.Should().BeEquivalentTo(expected);
}
}
。因此,您checkForMatch()
和undefined
的{{1}}和undefined
结束。因为cardsInPlay[0]
=== cardsInPlay[1]
,游戏认为它是匹配的。要解决此问题,请在完成<{1}}两次undefined
来电后,致电undefined
功能 。checkForMatch()
会自动调用...并且你永远不会改变游戏中的牌数。要解决此问题,请移除对flipCard()
的内部引用,并在第一次检查中运行第二个checkForMatch()
条件,即有两张卡片。这样,它只会在有两张牌时比较牌值。这可以在以下工作示例中看到:
checkForMatch()
希望这有帮助! :)
答案 2 :(得分:0)
两个观察结果:
您在调用checkForMatch
函数之前调用了函数flipCard
。
checkForMatch
length == 2
功能
console.log('up and running');
var cards = [
{
rank: 'queen',
suit: 'hearts',
cardImage: 'images/queen-of-hearts.png',
},
{
rank: 'queen',
suit: 'diamonds',
cardImage: 'images/queen-of-diamonds.png',
},
{
rank: 'king',
suit: 'hearts',
cardImage: 'images/king-of-hearts.png',
},
{
rank: 'king',
suit: 'diamonds',
cardImage: 'images/king-of-diamonds.png',
}
];
var cardsInPlay = [];
var checkForMatch = function() {
//if (cardsInPlay.length === 2)
//checkForMatch();
console.log(cardsInPlay)
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].rank);
console.log(cards[cardId].cardImage);
console.log(cards[cardId].suit);
cardsInPlay.push(cards[cardId].rank);
}
flipCard(0);
flipCard(2);
checkForMatch();
&#13;
快乐的编码!
答案 3 :(得分:0)
您在翻转功能之前调用了检查功能。在check函数中,两个索引都返回undefined值,这当然是相等的。