Is it possible to determine if a string contains a specific substring without using indexOf, regex match, or any standard JavaScript methods?
Feel free to review this code snippet on jsfiddle: https://jsfiddle.net/09x4Lpj2/
var string1 = 'applegate';
var string2 = 'gate';
function checkSubstring(string1, string2){
var j = 0;
var k = 0;
var contains = 'false';
var charArray1 = string1.split('');
var charArray2 = string2.split('');
for(var i = 0; i < charArray2.length; i++){
j = i;
if(charArray1[j++] != charArray2[k++]){
contains = 'false';
}else{
contains = 'true';
}
}
console.log(contains);
}
checkSubstring(string1, string2);
This approach works only when the indexes of the characters in both strings align (e.g., comparing "applegate" and "apple"). However, it fails when the indexes are not the same (e.g., comparing "applegate" and "gate"). How can the iteration values be manipulated to ensure accurate results in both scenarios?