So i try to call function isValid and it always returns invalid. I put a console.log before the return and it works.
valid is always undefined. What can i do to resolve this? I'm pretty noob with javascript. Everything works perfectly, just the function dosnt return the number 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;
}
}
}
});
}