Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm trying to run a program called IsSubArray. There are 2 input textboxes. One is called the big array and the other is called the sub array. The task is to check whether the values in sub array consecutively matches the numbers in big array. For example:
Array 1: 1,3,6,5,4
Array 2: 1,3,6 - true because 1,3 is matching with array 1.

Array 2: 1,6 - false because after 1 there is no 6 in array 1. It should match consecutively.

Array 2: 6,5,4 - true.

Array 2: 5,4,7 - false because is no number 7 after 4 in array 1.
But I am getting true for this last statement. I am unable to figure out why.

Thanks in advance :)


JavaScript
function IsSubArray() {
    inputArray1 = document.getElementById("inputText1").value.split(",");
    inputArray2 = document.getElementById("inputText2").value.split(",");

    var msg = false;
    for (j = 0; j < inputArray2.length - 1; j++) {
        if (j > 0 && msg == false)
            break;
        for (i = 0; i < inputArray1.length - 1; i++) {
            if (inputArray2[j] == inputArray1[i] && inputArray2[j + 1] != inputArray1[i + 1]) {
                msg = false;
                break;
            }
            else if (inputArray2[j] == inputArray1[i] && inputArray2[j + 1] == inputArray1[i + 1]) {
                msg = true;
                break;
            }
        }
    }
    document.getElementById("output").value = msg + " : LongArray: " + inputArray1 + " , ShortArray: " + inputArray2;
}


What I have tried:

So far everything works except for the last statement.
Posted
Updated 25-Oct-18 6:58am
v2
Comments
F-ES Sitecore 25-Oct-18 10:57am    
If you use the javascript debugger you can can step through your code line by line to see why it doesn't work. When comparing the last two items in the array as they match the first two items in the second array msg is set to true and nothing else sets it to false after.

I think you should come up with a simpler, more robust algorithm. Maybe loop array 1 until you find the first item in array 2, then have a loop that goes through the rest of array2 to make sure each elements match the next item in array 1 too.
W Balboos, GHB 25-Oct-18 12:53pm    
Aside: For the small arrays, shown, that would be OK. However, if the matching fails then the search needs to continue just past the last initial match to see if the substring still exists further down the big string. Just noting, really for the original questioner, that "it ain't over 'till it's over"

1 solution

Here's a back-handed solution for you:

Take the digits and turn them into a character strings so you have two strings:
'13654' and '136'.

Now, all you need do is use the php 'find-the-substring' function to see if the second string is in the first. You should still work on the logic of your original route as it's a good learning experience.
 
Share this answer
 
Comments
F-ES Sitecore 26-Oct-18 4:50am    
This is probably homework and the subject will be array handling so I doubt that would be accepted as a solution.
W Balboos, GHB 26-Oct-18 7:14am    
That's why I called it a back-handed solution, although I wasn't thinking of it as homework. Aside from that, neat way to recast the problem, no?
F-ES Sitecore 26-Oct-18 7:22am    
Not really, no. Let's say the array 1 is

1,5,8,12,45

and array 2 was
2, 4, 5

Your solution would result in a false positive.
W Balboos, GHB 26-Oct-18 7:23am    
Good catch. I guess he'll need to leave the commas in place! Meanwhile, tail between legs and grinning on this end.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900