Mini linter question

Hi there,

Is this a valid way to achieve the result of counting overused words? I felt like the if/else statements only help if you’re trying to access the individual overuse of the word. I came up with this solution that gives me the same result.

const countWords = (accumulator, curValue) => {
  return accumulator + curValue;
}

const lessWords = betterWords.map(word => {
  let i = 0;
  if (overusedWords.includes(word)) {
    i++;
  }
  return i;
})

console.log(lessWords.reduce(countWords))

I watched the video guide to double-check my work and they went with a longer code block of if/else statements. I’m just not sure if the route I took is fine or not. I’d prefer not to learn bad practice.

I took it a step further… so I can pass whatever array I want and have it perform the same action–sorry for the mess, kind of experimenting.

const countWords = (accumulator, curValue) => {
  return accumulator + curValue;
}


const countMoreWords = string => {
  const myCount = string.map(word => {
    let i = 0;
    if (overusedWords.includes(word)) {
      i++;
    }
    return i;
})
return myCount;
}

console.log(countMoreWords(betterWords).reduce(countWords))

Hi @keegankwirant8521787
generally I’d say the less code the better. And also the more obvious what your code does, the better. So I’d avoid the detour over the array of numbers, you have to reduce later on. With your variable i you can already count the words within your initial function and return the count straight away. So I’d rather suggest a solution, that makes use of the .includes() method as you do, but returns the count directly:

let i = 0;
const lessWords = () => {
betterWords.map(word => {
  if (overusedWords.includes(word)) {
    i++;
  }
})
return i;
}
1 Like