skip to Main Content

As you can see in below code. I’m trying to check whether mergeUserArray value is present in winArray or not. But I’m not able to achieve it.

Because I want, if any other number is in mergeUserArray it should match if three number combination present in it. Like 789 is present in 7189.

I tried regular expression too but it also didn’t work.
let regxWinArray = /(123)|(456)|(789)/g;

let winArray = [123,456,789];
// let mergeUserArray = [789];  --Value matched
 let mergeUserArray = [7189]; // Value didn't match
  if(winArray.includes(Number(mergeUserArray))){
    console.log("Number matched");
  }
  else {
    console.log("Number not matched");
  }

3

Answers


  1. let winArray = [123, 456, 789];
    let mergeUserArray = [7189];
    let matchFound = false;
    
    for (let i = 0; i < mergeUserArray.length; i++) {
      const num = mergeUserArray[i].toString();
      if (num.length >= 3) {
        for (let j = 0; j < num.length - 2; j++) {
          const combination = num.substr(j, 3);
          if (winArray.includes(Number(combination))) {
            matchFound = true;
            break;
          }
        }
      }
    }
    
    if (matchFound) {
      console.log("Number matched");
    } else {
      console.log("Number not matched");
    }
    
    Login or Signup to reply.
  2. Use global regex character match and check whether number of found characters match:

    let winArray = [123, 456, 789];
    
    // let mergeUserArray = [789];  --Value matched
    let mergeUserArray = [7189]; // Value didn't match
    
    let match = winArray.some(item => mergeUserArray.some(what => {
        const regex = new RegExp(`[${item}]`, 'g');
        return what.toString().match(regex)?.length === item.toString().length;
    }));
    
    console.log(match);

    If the order of characters should match:

    let winArray = [123, 456, 789];
    
    const hasMatch = mergeUserArray => winArray.some(item => mergeUserArray.some(what => {
        const regex = new RegExp(`[${item}]`, 'g');
        return what.toString().match(regex)?.join('') === item.toString();
    }));
    
    console.log([7189], hasMatch([7189]));
    console.log([1897], hasMatch([1897]));

    If we need all values in mergeUserArray matched:

    let winArray = [123, 456, 789];
    
    let mergeUserArray = [7189, 654]; 
    
    let match = mergeUserArray.every(what => winArray.some(item => {
        const regex = new RegExp(`[${item}]`, 'g');
        return what.toString().match(regex)?.length === item.toString().length;
    }));
    
    console.log(match);
    Login or Signup to reply.
  3. let winArray = [789, 456, , 123];
    let mergeUserArray = [7189];
    let matchFound = false;
    
    for (let item of winArray) {
        for (let item2 of mergeUserArray) {
            if (item && item.toString().localeCompare(item2.toString()) > -1) {
                console.log(`${item} is matched with ${item2}`)
                matchFound = true;
            } else {
                console.log('Not matched')
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search