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, its 3 in array 1 not 6. 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. I am just now learning algorithms.
Thanks in advance :)
function IsSubArray() {
inputArray1 = document.getElementById("inputText1").value.split(",");
inputArray2 = document.getElementById("inputText2").value.split(",");
var k = 0;
var msg = false;
for (j = 0; j < inputArray2.length - 1; j++) {
if (j > 0 && msg == false)
break;
//if (j > 0 && k == 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;
}
//if (inputArray2[j] == inputArray1[i])
// k = 1;
}
}
document.getElementById("output").value = msg + " : LongArray: " + inputArray1 + " , ShortArray: " + inputArray2;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input id="inputText1" type="text" />
<input id="inputText2" type="text" />
<br /><br />
<input id="IsSubArrayBtn" type="button" value="IsSubArray" onclick="IsSubArray()" />
<br /><br />
<input id="output" type="text" />
<script src="js/LogicalLoopings.js"></script>
</body>
</html>