Skip to content
Advertisement

Dynamically Find Values from a Variable

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.';


const storyWords = story.split(" ");
//console.log(storywords.length);

let overusedWords = ['really', 'very', 'basically'];

let WoolReally = 0;
let WoolVery = 0;
let WoolBasically = 0;

for(x of storyWords) {
  if (x === 'really'){
    WoolReally++;
  }
  else if (x === 'very'){
    WoolVery++;
  }
  else if (x === 'basically'){
    WoolBasically ++;
  }
}
console.log("Really was counted " + WoolReally + " times.");
console.log("Very was counted " + WoolVery + " times.");
console.log("Basically was counted " + WoolBasically + " times.");

Please can you help me try figure out how to dynamically check if a sentence includes any values that another variable includes and count that value.

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.';


const storyWords = story.split(" ");
//console.log(storywords.length);

let overusedWords = ['really', 'very', 'basically'];

let WoolReally = 0;
let WoolVery = 0;
let WoolBasically = 0;

for(x of storyWords) {
  if (x === 'really'){
    WoolReally++;
  }
  else if (x === 'very'){
    WoolVery++;
  }
  else if (x === 'basically'){
    WoolBasically ++;
  }
}
console.log("Really was counted " + WoolReally + " times.");
console.log("Very was counted " + WoolVery + " times.");
console.log("Basically was counted " + WoolBasically + " times.");

You can see that I have had to create a singular variable for each of the words that are part of the string for variable – overUsedWords in order to be able to count them in the loop and if else statement. There must be a way to beaten this up so I don’t have to do it with

let WoolReally = 0;
let WoolVery = 0;
let WoolBasically = 0;

Let me know what you think. I do apologise if this is super simple stuff. I’m just learning

Advertisement

Answer

   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.';


const storyWords = story.split(" ");

let overusedWords = ['really', 'very', 'basically'];

for(let word of overusedWords){
   let wordCount = storyWords.filter((storyWord) => {return storyWord === word;}).length
   console.log("The word '"+word+"' has been used "+wordCount+ " times" );
}

In this way you are simply filtering the list of the word obtained by the splitting of the story for each of the word inside your overusedWord list and returning the count of them. In this way if you want to add further words you just have to add them into your ‘overusedWord’ array and it will continue to work.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement