所以我尝试调用函数isValid,它总是返回无效。我在返回之前放了一个console.log,它可以工作。
有效总是未定义的。我该怎么做才能解决这个问题?我用javascript非常棒。一切都很完美,只是函数dosnt返回数字i。
var valid = isValid(houseNew, gameID, 1);
function isValid(card, gameID, ph) {
var cardnumbers = 0;
con.query("SELECT * FROM games WHERE id = " + gameID, function (err, result) {
if (err) throw err;
var cards = [];
cards.push(result[0].player1);
cards.push(result[0].player2);
cards.push(result[0].player3);
cards.push(result[0].player4);
cards.push(result[0].player5);
cards.push(result[0].house1);
cards.push(result[0].house2);
cards.push(result[0].house3);
cards.push(result[0].house4);
cards.push(result[0].house5);
for(var i = 0; i<10; i++)
{
if(cards[i] == card) {
cardnumbers++;
}
}
if(cardnumbers >= 4) {
return 0;
}
else {
if(ph == 0) {
var lastCard;
var i=0;
lastCard = cards[i];
i++;
while(i<=4 && lastCard != 0) {
lastCard = cards[i];
i++;
}
if(lastCard == 0) {
return i;
}
else {
return 10;
}
} else if (ph == 1) {
var lastCard;
var i=0;
lastCard = cards[i + 5];
i++;
while(i<=4 && lastCard != 0) {
lastCard = cards[i + 5];
i++;
}
if(lastCard == 0) {
return i;
}
else {
return 10;
}
}
}
});
}