Mini Linter Step 1. Help


#1

I had already completed this step and thought all was well until I came to step seven and tried to log it all into the console, I noticed that the betterWords array was full of random commas. I did not understand this so I looked back at the .split(" ") function and noticed that the number of extra commas changed with the number of spaces in the empty string of the .split() function. further I noticed that the number of spaces also effected the wordCount, sentenceCount and the overusedWords count. I am probably overlooking something obvious but please help!

let storyWords = story.split('   ');
let overusedWords = ['really', 'very', 'basically'];
let unnecessaryWords = ['extremely', 'literally', 'actually' ];


let betterWords = storyWords.filter(storyWord => !unnecessaryWords.includes(storyWord));
let overusedWordsFilter = storyWords.filter(storyWord => overusedWords.includes(storyWord));
let overusedWordCount = overusedWordsFilter.length;
let sentenceCount = 0;
betterWords.forEach(word => {
  if (word[word.length-1] === '.' || word[word.length-1] === '!') {
    sentenceCount+=1;
  }
});
    



let wordCount = storyWords.length;
console.log(`${sentenceCount} sentences`);
console.log(wordCount + ' words');
console.log(`${overusedWordCount} words to consider changing`);
console.log(betterWords.join());

#2

here:

let storyWords = story.split('   ');

how many spaces are in the string, and why did you choice this amount?


#3

I have been doing different amounts, first 2, then 4 then 0, and so on. it has always changed what is logged.
at first my choice of spaces was arbitrary because I did not know it’s significance.


#4

Trail and error can be a good approach, if it leads to what you need to know. You do need to figure out what the significance is.

otherwise, you could check documentation? MDN (mozzila has good documentation), so if you google something like: mdn split, you should get documentation about the function


#5

So it looks like my problem was with both the .split() and the .join() functions. I needed one space in each one. Thanks!


#6

yes, but do you understand why?


#7

Not exactly, to be honest I was having trouble understanding what the documentation was saying and how it related to the number of spaces in a string. It seems like each space changes the qualifications of what is to be split or joined.


#8

well, maybe we should then do an example which doesn’t uses spaces:

console.log("a-better-example".split('-'))

the character(s) (in this case -) determines where and when the string needs to be split.

join is the opposite, it determines what character(s) are used to join the array into a string


#9

Ah that makes so much sense, it only splits at the spaces, and we only want one space because otherwise it wouldn’t split, and that would effect all the counts by reducing the separate word strings. I still don’t understand where the commas came from initially however. My .join() didn’t even have a string in it before, does it insert commas by default?


#10

very good :slight_smile:

i wasn’t able to replicate that, so unable to explain.


#11

Ok, thanks very much for your help.


#12

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