Call this array 1:
["one","two","three","four","five"]
I have a second array, array 2, which is a sub-set of array 1. It might look like this:
["one","two"]
I now want to check whether the elements of array 2 are in the same order as the elements in array 1. It doesn't matter how many elements there are in array 2, however they need to be in the same order.
So for example:
["one","two"] ==> true
["two","three"] ==> true
["one","three"] ==> false
["two","one"] ==> false
I believe I have solved this, but I am wondering whether there isn't a better solution or way to approach it, and of course I'm wondering whether there are maybe hidden assumptions I shouldn't have made or errors I have committed. My solution looks like this:
let arr = ["s1","s2","s3","s4","s5","s6"];
let subArray = ["s4","s5"];
let counter = 0;
for(let i=0;i<=arr.length;i++) {
if(arr[i] === subArray[counter]) {
counter++;
} else if(counter>0) {
counter=0;
}
if(counter === subArray.length) {
console.log('consecutive');
return true;
}
}
if(counter !== subArray.length) {
console.log('not consecutive i guess');
return false;
}