I’m creating a word filter that if index 1 = dog and index 2 = cat, it will return true. What should I put in next index for word?
let textContainer = ['bird', 'dog', 'cat', 'snake', 'rabbit', 'ox', 'sheep', 'tiger'];
for (let word of textContainer) {
if (word === 'dog' && (next index for word) === 'cat') {
return true;
}
}
>Solution :
Use a normal for (let i = 0; i < textContainer.length; i++)
so you can check i + 1
let textContainer = ['bird', 'dog', 'cat', 'snake', 'rabbit', 'ox', 'sheep', 'tiger'];
function checkTextArray() {
for (let i = 0; i < textContainer.length; i++) {
if (textContainer[i] === 'dog' && textContainer[i + i] === 'cat') {
return true;
}
}
return false;
}
const res = checkTextArray()
console.log(res);