JavaScript实现getWinningLines函数:筛选达标中奖支付线
编写JavaScript函数getWinningLines(screen, payLines)
需求描述
编写JavaScript函数getWinningLines(screen, payLines),该函数接收两个数组参数,当screen对应索引与payLines的匹配数达到3个及以上时,返回符合条件的支付线。
规则说明
screen是二维整数数组,元素值为1-13,其中1和2为百搭(Joker),可匹配任意其他值;payLines是单元格坐标组成的二维数组。
示例
给定如下screen和payLines数组:
const screen = [ [9, 8, 1], [7, 5, 7], [6, 8, 7], [3, 9, 4], [6, 4, 3] ] const payLines = [ [[0, 2], [1, 2], [2, 2], [3, 2], [4, 2]], [[0, 2], [1, 0], [2, 2], [3, 0], [4, 2]], ]
函数应返回包含中奖信息的对象:
{ "0": { "win": "0.05", "positions": [[0,2],[1,2],[2,2]], "matches": 3 }, "1": { "win": "0.05", "positions": [[0,2],[1,0],[2,2]], "matches": 3 } }
原因:第一条支付线中,screen[0][2]为1(百搭)、screen[1][2]为7、screen[2][2]为7,共3个匹配;第二条支付线同理。
初始代码尝试
for (i=0; i<=(payLines.length)-1 ;i++){ let matches = 1 let line = [] line = line.concat(payLines[18]) // console.log(line) for (h=1; h<=(line.length)-1;h++){ const n = screenn[line[h][0]][line[h][1]] for (j=h+1; j<=(line.length)-1;j++){ if(n === screenn[line[j][0]][line[j][1]]){ matches= matches + 1 } } }console.log(matches)
内容的提问来源于stack exchange,提问作者Tarek Gharbi
相关产品推荐
相关产品推荐

