0

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;
  }
}

7
  • 3
    use a normal for(let i; i < ...) so you can do i + 1 Commented Jan 25, 2023 at 14:06
  • ... or use .forEach(), which passes the index to the callback. Commented Jan 25, 2023 at 14:06
  • @Pointy that can't work, note that the code tries to return. Commented Jan 25, 2023 at 14:07
  • 1
    @CherryDT oh right, well maybe .find() or .findIndex() then. Commented Jan 25, 2023 at 14:09
  • 1
    You want array some() Commented Jan 25, 2023 at 14:09

5 Answers 5

3

You can use Array.find or Array.some

find returns "dog" which is not undefined (so truthy) if dog is next to cat.

some will return true if dog, cat are found and false if not

Since your comments show you wanted a true/false function, I made you one.

const textArray = ['bird', 'dog', 'cat', 'snake', 'rabbit', 'ox', 'sheep', 'tiger'];

const areAdjacent = (word1, word2, arr) => arr
  .some((word, i, arr) => word===word1 && arr[i+1] === word2);

console.log(areAdjacent("dog","cat",textArray))
console.log(areAdjacent("tiger","bird",textArray))

Using Pilchard's example

const textArray = ['bird', 'dog', 'cat', 'snake', 'rabbit', 'ox', 'sheep', 'tiger'];

const areAdjacent = (word1, word2, arr) => arr
  .some((word, i, {[i+1]: nextWord}) => word === word1 && nextWord === word2);

console.log(areAdjacent("dog", "cat", textArray))
console.log(areAdjacent("tiger", "bird", textArray))

Sign up to request clarification or add additional context in comments.

3 Comments

this definitely looks more reading friendly. Although I'll have to study this first to better understand it. Thank you!
I did not know what you wanted with the return statement, I thought you had a function. I rewrote it as a function, I am using some because it returns true when found and false when not.
also somewhat opaque shorthand .some((word, i, {[i+1]: nextWord}) =>
2

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);

2 Comments

this is what I should have done on the first try. thank you!
or for (let [i, word] of textContainer.entries()) {
1

a for of loop is not going to let you do that. You could do it with a normal for or while loop, but arrays have built in methods that can make it easy to do.

You want to use some() which will allow you to get the index and reference the array you are looping over to get the next index.

let textContainer = ['bird', 'dog', 'cat', 'snake', 'rabbit', 'ox', 'sheep', 'tiger'];

const result = textContainer.some((text, index, array) => text === 'dog' && array[index+1] === 'cat'); 

console.log(result);

If you want to know where in the array it is, that would be findIndex()

let textContainer = ['bird', 'dog', 'cat', 'snake', 'rabbit', 'ox', 'sheep', 'tiger'];

const result = textContainer.findIndex((text, index, array) => text === 'dog' && array[index+1] === 'cat'); 

console.log(result);

Comments

0

You don't actually need explicit iteration at all. You can simply get the indexOf() the first word and then just access the next element.

let textArray = ['bird', 'dog', 'cat', 'snake', 'rabbit', 'ox', 'sheep', 'tiger',];

const areAdjacent = (word1, word2, arr) => {
  const i = arr.indexOf(word1);

  return i !== -1 && arr[i + 1] === word2;
};

console.log(areAdjacent('dog', 'cat', textArray));
console.log(areAdjacent('tiger', 'bird', textArray));

Or if you're wedded to a single line functional solution wrap it in an IIFE

let textArray = ['bird', 'dog', 'cat', 'snake', 'rabbit', 'ox', 'sheep', 'tiger',];

const areAdjacent = (word1, word2, arr) =>
  ((i) => i !== -1 && arr[i + 1] === word2)(arr.indexOf(word1));

console.log(areAdjacent('dog', 'cat', textArray));
console.log(areAdjacent('tiger', 'bird', textArray));

Comments

-1

You can use the indexOf method to return the index of a specific element, then use the index+1 to find the next element for the given one in the array.

The code could be like this:

let textContainer = 
[  "bird",
  "dog",
  "cat",
  "snake",
  "rabbit",
  "ox",
  "sheep",
  "tiger",];

for (let word of textContainer) {
  var index = textContainer.indexOf(word);
  if (word === "dog" && textContainer[index + 1] === "cat") {
    console.log("Cat is next of Dog!");
    // return true;
    //if you're inside a function.
  }
}

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.