Mini linter project / Extra tasks (last ones)

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

I wrote this in haskell… more for my own amusement, not particularly helpful

https://repl.it/repls/EthicalNoisyClasses

everyOther word
  | word `notElem` overusedWords = pure True
  | otherwise = do
    keep <- get <&> Map.findWithDefault True word
    modify $ Map.insert word (not keep)
    pure keep

it checks whether the word is in the overusedWords list – if not, then true (tell filter to keep it)

otherwise, it looks up the current value for that word in … a phone book
and immediately writes the inverse value back (true/false toggle)
the result is then whether to keep it (true/false)

this function is then given to filter, together with all the words

there’s no mention of any specific words

your code would look very different, because haskell is nothing like javascript… but it would involve the same thing. you’d have your phone book, you’d be looking up the current status for a word, toggling/incrementing, writing back, reporting true/false to filter

entirely useless as far as copying goes, but perhaps sightly useful to seeing what’s involved

2 Likes

I’ve read the documentation but I can’t understand how objects work…

I apologise for the trouble…I sincerely appreciate you trying to help me without giving me a direct answer (in my opinion the best way to learn), but i guess I’m not ready for this yet.

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;
    }
  }
});