Mini linter project / Extra tasks (last ones)

:rofl: That’s what i want, I’ll try and do that then…

Thanks for your help btw.

What do you think about this??

let newStory = []; 
let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;

const removeEveryOtherWord = betterWords.forEach(word => {
  if(!overusedWords.includes(word)) {
    newStory.push(word);
  } else {
    if(word === overusedWords[0]) {
      reallyCount++;
      if(reallyCount % 2 === 0) {
        newStory.push(word);
      } 
    } 
    if(word === overusedWords[1]) {
      veryCount++;
      if(veryCount % 2 === 0) {
        newStory.push(word);
      } 
    }
    if(word === overusedWords[2]) {
      basicallyCount++;
      if(basicallyCount % 2 === 0) {
        newStory.push(word);
      } 
    } 
  }
});

you’re not interested in locations 0 1 2, you’re interested in all
you’re not interested in very really basically, you’re interested in whatever is overused

Your earlier code can be squished down to:

let keep = false
const everyOtherWord = betterWords.filter(word =>
  !overusedWords.includes(word) || (keep = !keep)
})

and you’d continue to use filter for individual counts/toggles, since you’re answering a yes-or-no question for each word, which is what filter is for

the only difference is that instead of one toggle, you would have a collection of toggles

1 Like

I’m not able to understand the || (keep = !keep) part…

To be honest I’m not able to figure out how would that apply to the result i want… I started JS from scratch a month ago and trust me I’m trying my best! :rofl:

assignment is an expression whose result is the assigned value

console.log(x = 3)  // 3

This I don’t understand either… If i want to remove every other word of EACH overused word… This is the only way i can think about

know that so…

false = not false?

And for multiple counts you’d do the same thing. If you have a collection of entries that you make assignments to, that would be the same kind of expression. You’d do the lookup, toggle, assign back, read the assigned value.

1 Like

the left side of assignment is not a value

you’ve done assignment before, you don’t write:

5 = 3

when you use assignment as a statement or in a loop or whatever else, they’re all the same thing, it’s the same operator, same as how + doesn’t magically do different things in different situations

you can split it up on multiple lines, it’s just that I remove things that cancel out and end up with one line

1 Like

const everyOtherWord = betterWords.filter(word =>
!overusedWords.includes(word) || (keep = !keep)

Could you explain this with words?

Plus every time I .filter() something I’m creating a new array, but what i want is to eliminate words from the betterWords array right?

you could write the result back into your other array if you really wanted to (which I doubt)

as for the expression, pretty sure you know what each operation in that does

||
!
=
Array.prototype.contains
1 Like

I’m just not able to find another way to do it…

My logic is:

  1. Using .forEach in the betterWords array and checking if the word is included in the overusedWords array. If is not, I .push() it to my newStory array.

  2. If it is included, i check which word it is and depending on if it’s the one i should keep, keep it or not.

I guess you are suggesting another more efficient way of doing the same but im not able to understand it… I’d love to but i can’t.

use filter.

return true for keep, false for drop

…doesn’t get easier than that

1 Like
const everyOtherWord = betterWords.filter(shouldThisBeKept)

function shouldThisBeKept(word) {
  return true
}

compared to what you do with forEach, using filter is LESS, not more. like holding down backspace.

instead of appending/not appending, you return true/false, and you don’t need to create an empty array, you get the whole thing as the result.

1 Like

Ok thanks for that… I just don’t know how to apply the rule to each of the words in overusedWords if is not like this:

let reallyCount = 0;
let veryCount = 0;
let basicallyCount = 0;

const removeEveryOtherWord = betterWords.filter(word => {
  if(!overusedWords.includes(word)) {
    return true;
  } else {
    if(word === overusedWords[0]) {
      reallyCount++;
      if(reallyCount % 2 != 0) {
        return true;
      } 
    } 
    if(word === overusedWords[1]) {
      veryCount++;
      if(veryCount % 2 != 0) {
        return true;
      } 
    }
    if(word === overusedWords[2]) {
      basicallyCount++;
      if(basicallyCount % 2 != 0) {
        return true;
      } 
    } 
  }
});

if you have one thing, you can keep it in a variable.
if you have many things stick it into an array or some other container

my container = ......

for word from words
  current count = lookup(container, word)
  new count = current count + 1
  put(container, word, new count)

First of all thanks for your patience…

Second, I do not understand for what would I use those variables and for loop… I’m sorry! :rofl:

arrays let you hold on to a bunch of stuff.

you have a bunch of stuff. but there’s some organisation to it - you have a bunch of strings, and each string corresponds to a number

basically you need a phone book

a phone book is a data structure printed on paper, allowing fast lookups. you can do the same thing in computer memory

what data structures are there other than array?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

1 Like