简体   繁体   中英

find elements in array javascript

I want to write a javascript function called getWinningLines(screen, payLines) that gets as parameter 2 arrays and returns the matching lines between the indexes of $screen and the payLines when there are 3 or more matches.

Notes:

screen is an array of arrays of ints between 1 and 13 ( the index 1 and 2 are like a Joker, matches any other index ) payLines is an array of arrays of cells

Example:

For:

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]],
]

the solution getWinningLines(screen, payLines) should return:

{
"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
}
}

Why? we got as first index “1” because the first line in payLines ( which is payLines[0]) contains: [[0,2],[1,2],[2,2],[3,2],[4,2]] and inside screen we have: screen[0][2] = 1 (Joker) & screen[1][2] = 7 & screen[2][2] = 7 & screen[3][2] = 4(wrong)

So we have matches = 3 because the fourth is different, and positions = payLines[0]

we got as second index “2” because the 2nd line in payLines ( which is payLines[1]) contains: [[0,2],[1,0],[2,2],[3,0],[4,2]] and inside screen we have: $screen[0][2] = 1 (Joker) & screen[1][0] = 7 & screen[2][2] = 7 & screen[3][0] = 3(wrong)

So we have matches = 3 because the fourth is different, and positions = payLines[1]

i started by doing this:

 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)

ok, so all matching items should be either 1 or 2 or equal to each other? Let's try this then:

 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]], ] function countMatches(screen, payLine) { let value = 0, matches = 0 for (let [x, y] of payLine) { let s = screen[x][y] if (s === 1 || s === 2) { matches++ } else if (.value) { matches++ value = s } else if (s === value) { matches++ } else { break } } return matches } console,log(countMatches(screen, payLines[0]))

    function getWinningLines(screen, payLines) {
     let winningLines = [];
     payLines.forEach((line, index) => {
         let array = line.map((item) => screen[item[0]][item[1]]);
         let numberOfMatches = array.filter(item => (item === 1 || item === 2) 
         || array.indexOf(item) !== array.lastIndexOf(item)).length;
         if (numberOfMatches >= 3) {
          winningLines.push({
              "line": index + 1,
              "matches": numberOfMatches,
              "Array": array
          });
        
           }
   
         });
          return winningLines;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM