If word is present inside array. Create variable that stores the amount of times that word happens inside array. When I run this I get the error Unexpected token. If newStory[i] is equal to overusedWords[j] create a variable overusedWords[j] and add +1 to it.

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. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left 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 actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
let overusedWords = ['really', 'very', 'basically']
const newStory = story.split(" ");

//Here we find the number of times overusedWords are used specifically in story
for (let i = 0; i <= newStory.length; i++) {
  for(let j = 0; j <= overusedWords.length; j++){
 if(newStory[i] === overusedWords[j]){
   let overusedWords[j] = overusedWords[j] + 1;
 };
  };
};

2 Answers

2
wentjun On Best Solutions

That is because every element within the overusedWords array is a string. overusedWords[j] + 1 is simply string concatenation, and will not do anything useful that will achieve your objective.

The iteration for the element 'every' will only result in this: 'really11'.

Please do something like this instead:

const 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. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left 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 actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
const overusedWords = ['really', 'very', 'basically'];

const result = {};

const newStory = story.split(" ");

for (let i = 0; i < newStory.length; i++) {
  for(let j = 0; j < overusedWords.length; j++){
    if(newStory[i] === overusedWords[j]){
      if (result.hasOwnProperty(overusedWords[j])) {
        result[overusedWords[j]] = result[overusedWords[j]] + 1;
      } else {
        result[overusedWords[j]] = 1;
      }       
    };
  };
};

console.log(result);

Basically, we create an object that stores the number of times each of the keywords appear within that story.

3
Hien Nguyen On

You can not create variable with name contain [ character

You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types

let overusedWords[j] = overusedWords[j] + 1;

You can change to overusedWords[j] = overusedWords[j] + 1; for update array overusedWords at j index.

Also in your code should only check i < newStory.length instead of i <= newStory.length, this is a pattern for loop an array preventing index out of bound.