1

I'm having trouble figuring this one out. I have an array called answerArray and I need to see if it matches one of 4 other arrays (Some which are 2d) ex:

var answerArray = [1, 2, 2, 2, 1, 1];
var case1 = [
             [1, 2, 3, 1, 1, 1],
             [1, 2, 2, 2, 1, 1],
             [1, 2, 3, 2, 1, 1],
             [1, 2, 2, 1, 1, 1]
                               ];
var case2 = [1, 2, 3, 1, 1, 2];
var case3 = [
             [1, 2, 3, 1, 2],
             [1, 3, 2, 2, 2]
                            ];
var case4 = [1, 4];

In that example answerArray has a match with case1 because case1 contains [1, 2, 2, 2, 1, 1]

Here is a jsFiddle I did, seems like im close but still cannot get it: http://jsfiddle.net/dH2gx/1/

If you change answerArray to [1,4] you will see it says "Found Match on case 4"

Mario
  • 2,721
  • 1
  • 22
  • 23
  • 1
    Looks to me like you might need to call your function recursively such that if you have deep levels on nesting in an array which you are trying to evaluate, you can recursively call your function to look for a match. – Mike Brant Jul 17 '13 at 15:53
  • have a look at this answer: http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript – intuitivepixel Jul 17 '13 at 15:58
  • @intuitivepixel that is the exact way i am trying, doesn't work :/ – Mario Jul 17 '13 at 16:04
  • If you changed it to `[3, 2]` would it then be supposed to match case1 and case3 or must a whole row be equal? – Bergi Jul 17 '13 at 18:38

3 Answers3

0

surprisingly, you can use RegExp on numbers and arrays:

var answerArray = [1, 2, 2, 2, 1, 1];
var case1 = [
    [1, 2, 3, 1, 1, 1],
    [1, 2, 2, 2, 1, 1],
    [1, 2, 3, 2, 1, 1],
    [1, 2, 2, 1, 1, 1]
];
var case2 = [1, 2, 3, 1, 1, 2];
var case3 = [
    [1, 2, 3, 1, 2],
    [1, 3, 2, 2, 2]
];
var case4 = [1, 4];

var hit=new RegExp("^"+answerArray+"$", "m");
var names=['case1', 'case2', 'case3', 'case4'];
var res=[case1, case2, case3, case4].map(function(a,b,c){

   return hit.test(a.join("\n")) && names[b];

}).filter(Boolean);


alert(res) // shows: "case1"
dandavis
  • 16,370
  • 5
  • 40
  • 36
0

I figured it out!! For anyone in the future trying to do the same.

I made an array prototype "compare" seen here (SO Answer)

for (var k = 1; k <= 4; k++) {//Loop number of cases
    var caseArray = eval("case" + k);//To be able to loop through each case
    for (var i = 0; i < caseArray.length; i++) {
        if ($.isArray(caseArray[i])) { //Check if array[i] is another array (2d array)
            if (caseArray[i].compare(answerArray)) { //Compare the nested array
                console.log("MATCH " + k);
                return k;
            }
        } else {//Not a 2d array
            if (caseArray.compare(answerArray)) {//compare the array
                console.log("MATCH " + k);
                return k;
            }
        }
    }
}
Community
  • 1
  • 1
Mario
  • 2,721
  • 1
  • 22
  • 23
-1

NOOOO! EVAL()! MY NEMESIS!

Basically, the issue is that case4 is undefined. Your for loop uses <= so the iterator will go up to and include 4. When you enter your array comparison function, it doesn't enter the type-checking block because this is obviously not an instance of Array. But then, nothing causes it to return false.

Just move the type-checking up alongside the length comparison in arraysEqual, and return false. Oh, and...you're going to be asked many times not to use eval in your code. I'm going to assume that it was just a fast way of writing a test, though.

Katana314
  • 8,429
  • 2
  • 28
  • 36
  • Im starting i at 1, so case4 gets evaluated. Also, I know about everyone saying i shouldn't use eval. It does what i need in this situation so for now i will use it :) – Mario Jul 17 '13 at 16:03
  • Alright, there's some confusion because your fiddle is different from your question: There's no case4 in your question. – Katana314 Jul 17 '13 at 16:09
  • Yea, sorry about that. Fixed the question. – Mario Jul 17 '13 at 16:12