Mini linter project / Extra tasks (last ones)

const myCar = {}  // new empty object
myCar['make'] = 'Ford'  // associate a key with a value
console.log(myCar['make'])  // then fetch it
console.log(myCar['blah'] || 'default')  // you get undefined for missing keys

undefined is falsy, which is why you can use || to get either the value if there is one, or otherwise something else
though, it’s bit of a hack, because there might be legitimately falsy values in the object (for example 0, or false), and then you might instead want to do:

myCar.hasOwnProperty('blah') ? myCar['blah'] : 'default'

You COULD also use an array of pairs:

[['very', 0], ['really', 5]]

but to look anything up one would have to step through that and compare to the entry you’re looking for, which would be a really bad strategy for finding a number in a phone book

Ok Ok… I’m getting there…
How do I take info from betterWords array and implement it in the object?

let newObj = {}
newObj['really'] = 0;
newObj['very'] = 0;
newObj['basically'] = 0;

console.log(newObj)

const removeEveryOtherWord = betterWords.filter(word => {
  if(!overusedWords.includes(word)) {
    return true;
  } else {
    
  }
});

you wouldn’t mention specific words, you’d deal with it when you see them, whatever they may be

all you need from the object is to get and set values by key, there’s no particular relation to your array

1 Like

get (with default True)
set toggled value
return result
the key in question is the variable word

So does it mean that I have to use the object to include the overusedWords there?

If so, how would i do that from the removeEveryOtherWord function?

Then, when I create the object, which properties should i define?

I’ve been all day trying to figure this out… I can’t really think clearly and I’d like to get it done today :rofl:

if you encounter a word you have not seen before, what is then the count for that word, and what would you set as the new count?

Like this?

let newObj = {}

newObj['count'] = 0;

newObj['newCount'] = newObj['count'] + 1;

if you need specific variables you don’t need an object.

count = 0
newCount = count + 1

the problem you had was that you needed to track a count for each word

so, for some word, what is its count?

I guess the the count for every word would be 1 unless it is repeated later…

Is that what you mean?

Or would the count be 1 plus the ones after it?

but they change, so you can’t use 1, you would need to look it up

I don’t understand that… Myabe if you are a bit more specific about the step to follow i can finish this once and for all…

Honestly I just don’t understand it

you have many counts, one for each word

you need to organize those counts so that when you see a particular word, you could obtain the count for it

you could for example put the counts in an array together with the words so that you can tell which is which

but finding the right one would be a lot of work

Could you then tell me the best way to resolve it? I’ll understand it once i get it done…
I’ve been all day stuck on this part and I’d love to be able to understand it, learn it and finally get some rest…

Plese @ionatan could you explain how to use the object to store the info i want and how to find that infot to then eliminate the words i want?
Which variables ive got to set and why? (count, newCount)

you need pairs like those. one count per word. look at what objects are/do.

I think I made it work with an object as you first suggested…

What do you think about this??

let overusedWordsCount = {};
  overusedWordsCount['really'] = 0;
  overusedWordsCount['very'] = 0;
  overusedWordsCount['basically'] = 0;

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

yeah, though, you wouldn’t mention specific words.

it would for example break if “totally” was added to the list of overused words

the content of your function can be written as a single expression, if you have several booleans you can combine them using operations like and/or/not

1 Like

Hmmmmm :sweat_smile:

How would i do that?

add them when encountered

have I seen this before? no? then this is the first

1 Like

Ok… Is this correct?

I know you mention that I could shorten my code but I’m not comfortable doing that atm.

What about the code’s efficiency, what do you think about it? It took me a long time to solve this! :sweat_smile:

let overusedWordsCount = {};
let eachOverusedWord = overusedWords.forEach(overusedWord => {
  overusedWordsCount[overusedWord] = 0;
});

//console.log(overusedWordsCount);

const arrayWithRemovedWords = betterWords.filter(word => {
  if(!overusedWords.includes(word)) {
    return true;
  } else {
    overusedWordsCount[word]++;
    if(overusedWordsCount[word] % 2 != 0) {
      return true;
    } else {
      return false;
    }
  }
});

Now next step… find the most frequent word.

And thanks very much for your help!