JAVASCRIPT SYNTAX, PART II Mini Linter - its not printing the most overused word?

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-iterators/projects/mini-linter

The exercise for javascript is asking for me to print the most overused array, the logic makes but im not sure as to why it’s not printing. please help and if you require the whole code please ask.

betterWords.forEach(word =>{

  let temporary;

  if(word.endsWith(',') == true)
  {
    temparary = word.slice(0,-1);
  }

  else if(word.endsWith('.') == true)
  {
    temparary = word.slice(0,-1);

  }

  else if(word.endsWith('!') == true)
  {
    temparary = word.slice(0,-1);

  }
  
  else 
  {
    temparary = word;
  }

  if(temparary === overusedWords[0])
  {
    countOne++;
  }


  else if(temparary === overusedWords[1])
  {
    countTwo++;
  }

  else if(temparary === overusedWords[2])
  {
    countThree++;
  }

  if(countOne > countTwo && countOne > countThree)
  {
    console.log(`The word appears the most is ${overusedWords[0]}`);
  }


  else if(countTwo > countOne && countTwo > countThree)
  {
    console.log(`The word appears the most is ${overusedWords[1]}`);
  }

  if(countThree > countTwo && countThree > countOne)
  {
    console.log(`The word appears the most is ${overusedWords[2]}`);
  }

});

Hi,
yes, in order to help you, I think we need more information:

  1. Do you get an error message?
  2. How are the variables const betterWords, overusedWords, countOne, countTwo and countThree initialized?

If I add the declarations of these variables to your code, it prints something:

const betterWords = ['better', 'words', 'array'];
const overusedWords = ['array'];
let countOne = 0;
let countTwo = 0;
let countThree = 0;

Sorry, I was in a hurry but the variables are were not initialized, they were just declared. This is my whole code.

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

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

const storyWords = story.split(' ');

//console.log(storyWords.length);

const betterWords = storyWords.filter(word =>{

  return !unnecessaryWords.some(unnecessaryWord =>{
    return unnecessaryWord === word;
  });
  
});


//console.log(betterWords);
let i = 0;
let j = 0;
let k = 0;

const amountOfWords = betterWords.forEach(word =>
{
  
  if(overusedWords[0] === word)
  {
    //let i = 0;
    //console.log(i);
    i = i + 1;
  }

  if(overusedWords[1] === word)
  {
    //let i = 0;
    //console.log(i);
    j = j + 1;
  }

  if (overusedWords[2] === word)
  {
    //let i = 0;
    //console.log(i);
    k = k + 1;
  }



});


let sentences = 0;

let amountOfSentences = betterWords.forEach(sentence => {

  if(sentence.endsWith('.') == true)
  {
    sentences += 1;
  }

  if(sentence.endsWith('!') == true)
  {
    sentences += 1;
  }

});


console.log(`The amount of times the word ${overusedWords[0]} has been used is: ${i}`);
console.log(`The amount of times the word ${overusedWords[1]} has been used is: ${j}`);
console.log(`The amount of times the word ${overusedWords[2]} has been used is: ${k}`);

console.log(`The amount of sentences is:  ${sentences} `);

console.log('The original story contains ' + storyWords.length + ' words\n');

//console.log(betterWords.join(' '));

storyWords.forEach(word =>{
 if(overusedWords[0] === word)
 {
   betterWords.splice(betterWords.indexOf(word),1,'shiii');
 }

 else if(overusedWords[1] === word)
 {
      betterWords.splice(betterWords.indexOf(word),1,'shiii');
 }

  else if(overusedWords[2] === word)
 {
      betterWords.splice(betterWords.indexOf(word),1,'shiii');
 }

});

//console.log(betterWords.join(' '));

let countOne = 0;
let countTwo = 0;
let countThree = 0;


betterWords.forEach(word =>{

  let temparary;

  if(word.endsWith(',') == true)
  {
    temparary = word.slice(0,-1);
  }

  else if(word.endsWith('.') == true)
  {
    temparary = word.slice(0,-1);

  }

  else if(word.endsWith('!') == true)
  {
    temparary = word.slice(0,-1);

  }
  
  else 
  {
    temparary = word;
  }

  if(temparary === overusedWords[0])
  {
    countOne++;
  }


  else if(temparary === overusedWords[1])
  {
    countTwo++;
  }

  else if(temparary === overusedWords[2])
  {
    countThree++;
  }

  if(countOne > countTwo && countOne > countThree)
  {
    console.log(`The word appears the most is ${overusedWords[0]}`);
  }


  else if(countTwo > countOne && countTwo > countThree)
  {
    console.log(`The word appears the most is ${overusedWords[1]}`);
  }

  if(countThree > countTwo && countThree > countOne)
  {
    console.log(`The word appears the most is ${overusedWords[2]}`);
  }

});



This purpose of for each loop is to first remove the punctation of the word then compare to an array called overused words to figure what the greastest overused word is.

nevermind fix it, lmao

You code runs without an error thrown and it logs a valid result, right? So what are you expecting to happen that doesn’t happen?

The forEach method doesn’t return anything. So amountOfWords and amountOfSentences are undefined.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.