A
A
Andrey Solovyev2013-12-04 21:28:48
JavaScript
Andrey Solovyev, 2013-12-04 21:28:48

How to do pattern validation in JavaScript?

Hello.
Need help. See an example:

var listResult = new Array(
  [x,x,x,0,0,0,0,0,0],
  [0,0,0,x,x,x,0,0,0],
  [0,0,0,0,0,0,x,x,x],
  [x,0,0,x,0,0,0,x,0],
  [0,x,0,0,x,0,0,x,0],
  [0,0,x,0,0,x,0,0,x],
  [x,0,0,0,x,0,0,0,x],
  [0,0,x,0,x,0,x,0,0]
);

var result = [0,2,0,1,1,1,0,2,2]; // == listResult[1]
var result2 = [1,0,2,1,2,0,2,0,1]; // == listResult[7]

You need to check for the occurrence of result or result2 in listResult.
For example result should match listResult[1] and result2 match listResult[7] ?
4917590.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Guketlev, 2013-12-04
@AndreySolo

What is your verification principle? How does [0,2,0,1,1,1,0,2,2] match [0,0,0,x,x,x,0,0,0]?
0 - any character x - same?
If the patterns do not change - write down the rules in code - a[3]==a[4] && a[4]==a[5]. If they change, then you have to iterate through the array, select all indices with x, and compare all the values ​​of the target array. You can "precompile" the rules into a function if you want the check to be optimized. To do this, generate the text of the function (directly the line if(a[3]==a[4] && a[4]==a[5]) return true; else return false; and create a function based on it: javascript.ru/ function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question