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 :)
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.