Coding linting with JS to iterate through a text.
I have a few iterations that count a specific conditions, for example
sentences
are set to be added upon when there is ‘.’ or ‘!’.
However, when I created a function that would print one string with information about number of sentences, overused words and so on, it shows up as undefined… Here’s a piece of code
console.log(betterWords.length);
let counter = 0;
for (let i = 0; i < betterWords.length; i++){
if (betterWords[i] === 'really' || betterWords[i] === 'very' || betterWords[i] === 'basically'){
counter++
}
}
console.log('You used overused words ' + counter + ' times.');
let sentences = 0;
betterWords.forEach (word => {
if (word[word.length-1] === '.' || word[word.length-1] === '!'){
sentences++
}
});
console.log('There are ' + sentences + ' sentences');
numberOfWords = betterWords.length;
const printFunction = (betterWords, counter, sentences) => {
return('The number of words in text is ' + numberOfWords + ' There are ' + sentences + ' and overused words are used ' + counter + ' times.');
};
console.log(printFunction());
Output
182
You used overused words 8 times.
There are 12 sentences
The number of words in text is 182 There are undefined and overused words are used undefined times. I am mentioning sentences as one example here. One can see that numberOfWords give a proper output
As one can see once sentences return 12, the other time it’s undefined.