Grammar Check- finding multiple indicies using one function

How do i call multiple indicies inside one array with only one function? Bottom line: i want to find two elements within an array using one function, then be able to change the value of those elements inside the same function.

(https://www.codecademy.com/courses/introduction-to-javascript/projects/mini-linter)

I want to be able to find the index for both elements, ‘breathtaking’ and ‘freaking’ inside the story array. Currently i am getting only one element returned. Wondering if is because of the .findIndex that once it finds one truthy it returns that value and does not let the second itireation happen?

let story = 'Last weekend, I took literally the most beautifull bike ride of my life. 
The route is called "The 9W to Nyack" and it stretches all the way from Riverside
 Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! 
It is a 48 mile loop and it literally took me an entire day. I stopped at Riverbank State 
Park to take some artsy photos. It was a short stop, though, because I had a freaking long way to go. 
After a quick photo op at the very popular Little Red Lighthouse I began my trek across the George Washington Bridge into New Jersey. The GW is a breathtaking 4,760 feet long!
 I was already very tired by the time I got to the other side.
 An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautifull park
 along the coast of the Hudson. Something that was very surprising to me was that near
 the end of the route you literally cross back into New York! At this point, you are very 
close to the end.';
let storyWords = story.split(' ');

let unnecessaryWord = 'literally';

let misspelledWord = 'beautifull';

let badWord = 'freaking';

let count = 0

let longWord = 'breathtaking';

let  badWordIndex = storyWords.findIndex((word) => {
 return word === badWord 
})

let badAndLongWord = storyWords.findIndex((word) => {
  if (word === 'freaking') {
    return word
  } if (word === 'breathtaking') {
    return word
  }
})
console.log(badAndLongWord)

Yes. Checking the documentation reveals what the .findIndex() method promises to do.

You could write your own method to return the index of each of those words.

1 Like

Ok, fair enough. Thanks for the clarification

1 Like

I see that what you are wanting to do is outside the bounds of the project which is awesome! Thinking beyond the instructions and expanding on these projects is a great way to further your understanding. I’ll share what I came up with to do what you are suggesting, but try coming up with your own method as well. Happy coding!

Code
const wordsToFind = ['freaking', 'breathtaking']
const indicesOfWords = [];
storyWords.forEach((word, index) => {
  if(wordsToFind.includes(word)) {
    indicesOfWords.push(index);
  }
})
console.log(indicesOfWords);
indicesOfWords.forEach(i => console.log(storyWords[i])); //just to make sure I found the right words

Output:

[ 80, 113 ]
freaking
breathtaking

1 Like