我正在尝试创建一个 Javascript 纸牌游戏,但我需要匹配列表中的 4 个后续数字。但是我总是创建一些疯狂的分层循环,例如:
cards = [{card:'h7'},{card:'c8'},{card:'h9'},{card:'st'}]
var sorted = ['7','8','9','t','j','q','k','a']
var found4 = false
for(var i =0;i < 5;i++){
var found = 0;
for(var j = 0;j < 4;j++){
for(var c in cards){
if(cards[c].card.charAt(1) == sorted[i+j]){
found++
}
}
}
if(found == 4){
found4 = true
}
}
有更好的方法来匹配数组吗?
一些输入示例:
'7','8','9','t' => true
'j','q','k','a' => true
'7','8','k','a' => false
'j','k','7','a' => false
(输入未排序)