lyrically wicked - 2 months ago 6
Javascript Question

What would be an example of the function working with an array of arrays with some logic?

I have a function that takes two arguments, e.g.,

``````function test(a, b) {
if (a == b) return true;
return false;
};
``````

What I want is to have a function
`func(arg)`
that returns
`false`
by default. It takes one argument, and this argument will be an array of arrays. Each instance of the function will be started with some dynamically generated number (integer), let's call it
`myNumber`
and let's say it's just some random integer from 0 to 10. This function should do the following:

``````var myNumber = randomIntegerFrom0To10;
var tmp = 0;
test(arg[0][0], arg[1][0]);
test(arg[0][0], arg[1][1]);
test(arg[0][0], arg[1][2]);
...
``````

and as soon as
`test(...)`
returns
`true`
, set
`tmp++`
and go to

``````test(arg[0][1], arg[1][0]);
test(arg[0][1], arg[1][1]);
test(arg[0][1], arg[1][2]);
....
``````

and as soon as
`test(...)`
returns
`true`
, set
`tmp++`
and go to

``````test(arg[0][2], arg[1][0]);
test(arg[0][2], arg[1][1]);
test(arg[0][2], arg[1][2]);
...
``````

...and so on, until

``````test(arg[0][arg[0].length-1], arg[1][0]);
test(arg[0][arg[0].length-1], arg[1][1]);
test(arg[0][arg[0].length-1], arg[1][2]);
...
``````

— as always, as soon as
`test(...)`
returns
`true`
, set
`tmp++`
, but after
`test(arg[0][arg[0].length-1], arg[1][arg[1].length-1])`
, we must check if
`tmp`
is equal to
`myNumber`
. If yes, the function should return
`true`
and stop; but if not, we must continue (note that we always set
`tmp = 0`
after each comparison with
`myNumber`
):

``````tmp = 0;
test(arg[0][0], arg[2][0]);
test(arg[0][0], arg[2][1]);
test(arg[0][0], arg[2][2]);
...
``````

and as soon as
`test(...)`
returns
`true`
, set
`tmp++`
... and so on. If we never see that
`tmp`
equals to
`myNumber`
, we will eventually come to

``````test(arg[0][0], arg[arg.length-1][0]);
test(arg[0][0], arg[arg.length-1][1]);
test(arg[0][0], arg[arg.length-1][2]);
``````

continuing the logic described above. The longest way here will be to go to

``````test(arg[0][(arg[0].length-1)], arg[arg.length-1][(arg[arg.length-1].length-1)]);
``````

and to check the current
`tmp`
. If it equals to
`myNumber`
, the function should return
`true`
and stop, but if not, we must continue:

``````tmp = 0;
test(arg[1][0], arg[2][0]);
test(arg[1][0], arg[2][1]);
test(arg[1][0], arg[2][2]);
...
``````

...continue until

``````test(arg[1][(arg[1].length-1)], arg[arg.length-1][(arg[arg.length-1].length-1)]);
``````

and check the current
`tmp`
. As always, either return
`true`
or continue:

``````tmp = 0;
test(arg[2][0], arg[2][0]);
test(arg[2][0], arg[2][1]);
test(arg[2][0], arg[2][2]);
``````

...and so on. The longest theoretically possible way will be to go to

``````test(arg[arg.length-2][(arg[arg.length-2].length-1)],
arg[arg.length-1][(arg[arg.length-1].length-1)]);
``````

and, if current
`tmp`
equals to
`myNumber`
, return
`true`
. Otherwise, return
`false`
and finally stop.

Example:

``````var myNumber = 1;
var tmp = 0;
var input1 = [ [ 4, 5 ],
[ 3, 2, 8, 7, 1, 10 ],
[ 9, 4, 8, 50 ],
[ 10, 20, 30]
];

// 4 !== 3; tmp == 0;
// 4 !== 2; tmp == 0;
// 4 !== 8; tmp == 0;
// ...4 !== 10 ; tmp == 0;
// 5 !== 3 ; tmp == 0;
// 5 !== 2 ; tmp == 0;
// ...5 !== 10 ; tmp == 0; is the current tmp equal to myNumber? No. So, continue:
// 3 !== 9 ; tmp == 0;
// 3 !== 4 ; tmp == 0;
// 3 !== 8 ; tmp == 0;
// 3 !== 50 ; tmp == 0;
// 2 !== 9 ; tmp == 0;
// ...2 !== 50 ; tmp == 0;
// 8 !== 9 ; tmp == 0;
// 8 !== 4 ; tmp == 0;
// test(8,8) returns true; set tmp++ and, since there is no need for test(8,50), go to
// 7 !== 9 ; tmp == 1;
// ...7 !== 50 ; tmp == 1;
// 1 !== 9 ; tmp == 1;
// ...10 !== 50; tmp == 1; is the current tmp equal to myNumber? Yes.
// So, return true and stop.
``````

If we had the input like this:

``````var input2 = [ [ 1, 2 ],
[ 3, 4, 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15]
];
``````

then we would have to perform the maximum possible amount of "test" operations and return
`false`
after
`test(12,15`
).

The question is: what would be an example of any working function, such that, following the above logic, will do the actions described for the two given examples of inputs:
`func(input1)`
and
`func(input2)`
?

I used a logic of combining rest of the arrays into a single array to compare, that also reduce the logic i need to write.

``````function traverseForMyNumber(){
var myNumber = 1;
var tmp = 0;
var input = [ [ 4, 5 ],
[ 3, 2, 8, 7, 1, 10 ],
[ 9, 4, 8, 50 ],
[ 10, 20, 30]
];
for(var i = 0; i< input.length; i++){
var copyArray = input.slice();
var tempArray  = copyArray.splice(i, 1);

//Concat rest of the arrays into single arary for easy traversal
copyArray = [].concat.apply([], copyArray);

//Logic for incrementing tmp
for(var j = 0; j< tempArray[0].length; j++){
for(var k = 0; k < copyArray.length; k++){
if(test(tempArray[0][j],copyArray[k])){
tmp++;
if(tmp === myNumber) return true;
}
}
}
}
}

function test(a, b) {
return a === b;
}

traverseForMyNumber();
``````