0

I don't see what the problem is. The same type of code worked fine for something similar before (jsfiddle https://jsfiddle.net/Montinyek/ufkdgz4t/3/), but now it just gives a strange error. Can someone please explain why this method worked for the jsfiddle example but not here?

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually 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 basically took me an entire day.

let unnecessaryWords = ['extremely', 'literally', 'actually' ];
const betterWords = []
const storyWords = story.split(' ')

for(let i = 0; i < storyWords.length; i++) {
  for(let j = 0; j < unnecessaryWords.length; j++) {
    if (storyWords[i] !== unnecessaryWords[j]) {
      betterWords.push(storyWords)
    }
  }
}

I also tried using the splice() method, again with weird results:

for(let i = 0; i < storyWords.length; i++) {
  for(let j = 0; j < unnecessaryWords.length; j++) {
    if (storyWords[i] === unnecessaryWords[j]) {
      betterWords.push(storyWords.splice(unnecessaryWords[j], 1))
    }
  }
} 
5
  • 2
    a strange error please be more specific. weird results there too. Commented Mar 18, 2022 at 17:44
  • Actually, the error was due to my duplicate usage usage of i instead of j, so now there's no error. But the result of the first code is storyWords that repeats itself a bunch of times, there are hundreds of words there now. Commented Mar 18, 2022 at 17:52
  • Are you trying to create an array of storyWords that don't appear in unnecessaryWords? Commented Mar 18, 2022 at 17:56
  • Yes. storyWords should not include unnecessaryWords. I know there are other ways to achieve this, but I'm just curious why does this method in particular not work? Commented Mar 18, 2022 at 17:58
  • One reason is because although storyWords[i] might not equal unnecessaryWords[j], it might equal the next unnecessaryWord from the array. Basically for each storyWord, check that it doesn't exist in unnecessaryWords. Commented Mar 18, 2022 at 18:00

2 Answers 2

1

As you mentioned there are other ways to achieve this. Here is one of them.

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually 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 basically took me an entire day.';

let unnecessaryWords = ['extremely', 'literally', 'actually' ];

const storyWords = story.split(' ')

const betterWords = storyWords.filter(sw => !unnecessaryWords.includes(sw));

console.log(betterWords);

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

4 Comments

Thank you. Yes that's a better way to do it, but I'm wondering what the issue with my code is? Because as far as I know, the logic is correct.
The main problem is the inner if - say i is 4 and j is 0. That yields the story word "literally" and the unnecessary word "extremely". Literally is not equal to extremely, so the word literally gets added to better words. You should only add the story word to better words if NONE of the unnecessary words match it.
"You should only add the story word to better words if NONE of the unnecessary words match it." And is there a way to achieve that using a for loop?
Check out answer #2.
0

You can create a variable which keeps track of whether the particular storyWord was found in unnecessaryWords, and only push the storyWord to betterWords if it's false.

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually 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 basically took me an entire day.';

let unnecessaryWords = ['extremely', 'literally', 'actually' ];
const betterWords = []
const storyWords = story.split(' ')

for(let i = 0; i < storyWords.length; i++) {
  let foundInUnnecessary = false;
  for(let j = 0; j < unnecessaryWords.length; j++) {
    if (storyWords[i] === unnecessaryWords[j]) {
      foundInUnnecessary = true;
      break;
    }
  }
  if (!foundInUnnecessary) {
    betterWords.push(storyWords[i]);
  }
}

console.log(betterWords);

2 Comments

@JamesThank you! Just one more question, if I simply do this if (storyWords[i] === unnecessaryWords[j]) It will push every instance of unnecessaryWords in storyWords into betterWords, so why is the opposite effect not achieved if I change === to !== , why doesn't that simply filter storyWords and return it without the strings found in unnecessaryWords? Instead it still returns them and duplicates the non-matching words by the number of strings in unnecessaryWords. I'm just trying to understand what causes this behavior.
The logic you need is "does this word occur anywhere in unnecessaryWords". You need to go through all of the unnecessaryWords, and only quit once you either find it or reach the end, in order to know "does this word occur anywhere in unnecessaryWords". Testing the jth unnecessaryWord, and acting only on that, is not sufficient, regardless of === or !==

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.