-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeGridSearch.js
59 lines (57 loc) · 1.83 KB
/
theGridSearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Complete the 'gridSearch' function below.
*
* The function is expected to return a STRING.
* The function accepts following parameters:
* 1. STRING_ARRAY G
* 2. STRING_ARRAY P
*/
function gridSearch(G, P) {
// Write your code here
let isYes = false;
let resTab = [];
let lengthP = P.length;
let lengthPElement = P[0].length;
let tempFirst = "";
let tempString = "";
for(let i=0; i<G.length; i++){
for(let j=0; j<G[i].length; j++){
if(G[i][j] == P[0][0]){
console.log('G[i][j]----------', G[i][j]);
console.log('j = = = =', j);
console.log('i = = = =', i);
console.log('P[0][0]----------', P[0][0]);
tempFirst = G[i].slice(j, lengthPElement+j);
console.log('tempFirst = = = = ', tempFirst);
if(tempFirst == P[0]){
for(let k=i; k<lengthP+i; k++){
tempString = G[k].slice(j, lengthPElement+j);
resTab.push(tempString);
}
console.log(resTab);
isYes = (resTab.length == P.length) && resTab.every(function(element, index) {
return element === P[index];
});
if(isYes){
break;
}else{
resTab = [];
}
}
}
}
if(isYes){
break;
}
}
if(isYes == true){
return 'YES';
}else{
return 'NO';
}
}
// let G = ["1234567890", "0987654321", "1111111111", "1111111111", "2222222222" ]; //YES
// let P = ["876543", "111111", "111111"];
let G = ["123412", "561212", "123634", "781288"];
let P = ["12", "34"];
console.log(gridSearch(G, P));