Mini Linter - step 8

Thanks for (re)opening this topic. I am stuck at step 8 too. I finaly started to understand Javascript. until this point. What do you do when you get stuck ? How to stay motivated?

This step is frustrating because I don’t know how to apply without any help. :slightly_frowning_face:

1 Like

Honestly, as a beginner myself, the best piece of advice I could give you (not just for coding, really it can be applied to anything) is just don’t be afraid to get stuck, and then spend time on it. An extract from an article I read really helped me understand why (and trust me when I say it definitely works! I probably spent hours, on each challenge, more than a day on the second one, and while that can seem discouraging, I felt like I was really learning because I truly had to understand what I was typing to actually come up with something) :

“In a variety of brain-mapping studies, scientists discovered that when faced with a problem, the left hemisphere of the brain—the side of the brain typically associated with analytical problem solving—got right to work. However, the tricky problems the scientists developed quickly wore out the left-brain thought process. The subjects became frustrated and complained about the unsolvable problems. This frustration was crucial because it signaled a need for an alternative method, and scientists saw a shift in activity from the left side of the brain to the right side—the more “creative” side—of the brain. Once activity shifted to the right side, subjects often experienced these moments of insight, a flash of activity in the brain resulting in them bolting upright in their chairs as the answer appeared to them. They had to get stumped in order to get creative and solve the problem.”

You should check out the full article here, I personally found it very useful: https://greatergood.berkeley.edu/article/item/five_tips_for_reaching_your_creative_potential

I hope this helped in any way! Don’t get discouraged, we’re learning together :smile: , you can do it! The longer you spend understanding right now, the easier it will be to actual make use of all your knowledge later on :slight_smile:

There’s nothing wrong with seeking the forums/internet for help! It can really be helpful when looking at tackling a problem using different methods. Just make sure you spend as much time as you can trying to solve it yourself first!

Also! Personal preference, but since the lessons teach so much, I find it very useful to use some handwritten notes just to gather what I’ve learnt in one place and have access to it when I’m trying to solve something.

2 Likes

Hi! Thanks for the motivation! You might have hit the nail on the head. I have a brain injury that probably affects the left side of my brain. Your explanation is very helpful. Thanks!

still confuse none code works help!!!

Hi. I came up with a working solution for extra task #3.

// #3

const reallyReplacements = ['truly', 'absolutely', 'certainly'];
const veryReplacements = ['tremendously', 'greatly', 'decidedly'];
const basicallyReplacements = ['essentially', 'fundamentally', 'primarily'];

const replacedWords = [];

const replaceWords = betterWords.forEach((word) => {
  let randomIndex = Math.floor(Math.random()*4);
  if (word === 'really') {
    replacedWords.push(reallyReplacements[randomIndex]);
  } else if (word === 'very') {
    replacedWords.push(veryReplacements[randomIndex]);
  } else if (word === 'basically') {
    replacedWords.push(basicallyReplacements[randomIndex]);
  } else {
    replacedWords.push(word);
  }
})

console.log(replacedWords.join(' '));

However, I’m stuck on #2 of task 8 for a few days now, and I feel really dumb because of that. So far, I stole the .reduce() solution for finding the number of occurrences of each word, from another thread :stuck_out_tongue: . It returns an object, and I don’t know what do do with it (haven’t revised objects yet, and I think I shouldn’t need it for the iterators exercise).
So I have a mental block on the simplest step I think, which is actually finding the most used word.
I would appreciate some guidance, as some solutions from this thread don’t work for me, they just throw type error even after tinkering with the code. Maybe suggestions for a different approach? I really want to get the iterators.

Anyway, thanks to everyone contributiong!

OK, I figured it out after leaving this project for a few days, and learning about objects. It amazes me every time, that the solution is so incredibly simple, yet I wasn’t able to do it.

const frequencyCounter = () => {  
  return zeroPunctuation.reduce((countMap, word) => {
    countMap[word] = ++countMap[word] || 1;
    return countMap;
  }, {}) 
};

const lexicon = frequencyCounter();

const theSearchForTheMostCommonWord = () => {
  let maxCount = 0;
  let maxWord = '';
  for (let key in lexicon) {
    if (lexicon[key] > maxCount) {
      maxCount = lexicon[key];
      maxWord = key;
    }
  }
  console.log(`The most frequent word is "${maxWord}", which was used ${maxCount} times.`);
}
theSearchForTheMostCommonWord();

I hope it helps anybody, who is struggling with this.

I think this would work for task number 2 :

 distribution = {};
    max = 0;
    result = [];

storyWords.forEach(function (a) {
    distribution[a] = (distribution[a] || 0) + 1;
    if (distribution[a] > max) {
        max = distribution[a];
        result = [a];
        return;
    }
    if (distribution[a] === max) {
        result.push(a);
    }
});
console.log( result + ' is repeated ' + max + ' times');

I tried really hard to just use what we’ve learned so far, and I found solutions! Overly complicated, that’s for sure, but it helped me review the lessons. I hope these can help others who got stuck like me.

If anyone has questions on specifics, let me know and I’ll try my best to explain!

Task 1: For the overused words, remove it every other time it appears.

// First, count overused words (This was also my solution to step 4)
overusedWords.forEach(word => {
  let count = 0;
  for (let i = 0; i < betterWords.length; i++) {
    if (betterWords[i] === word) {
      count++
    };

// Second, remove overused words every other time
    if (betterWords[i] === word && count % 2 === 0) {
      betterWords.splice(i, 1);
    }
  };
  console.log(`"${word}" count: ${count}`);
});

Task 2: Write a function that finds the word that appears the greatest number of times.

// First, account for capitalized words and remove special characters
let formattedWords = [];
let letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
betterWords.forEach(betterWord => {
  betterWord = betterWord.toLowerCase();
  betterWord = Array.from(betterWord);
  const allLetters = betterWord.filter(element => letters.includes(element));
  betterWord = allLetters.join('');
  formattedWords.push(betterWord);
});

// Second, create an array made up of each word's frequency
let mostCommonCount = [];
formattedWords.forEach(formattedWord => {
  formattedWordCount = 0;
  for (m = 0; m < formattedWords.length; m++) {
    if(formattedWords[m] === formattedWord) {
      formattedWordCount++
    }
  }
  mostCommonCount.push(formattedWordCount);
});

// Third, match the number of the highest word frequency with the word itself
let mostCommonWordCount = Math.max(...mostCommonCount);
let mostCommonWord = betterWords[mostCommonCount.indexOf(mostCommonWordCount)];

console.log(`The most common word is "${mostCommonWord}" which appears ${mostCommonWordCount} times`);

Task 3: Replaced overused words with something else.

// First, create an array of replacement words
let replacementWords = ['particularly', 'extraordinarily', 'pretty', 'so', 'almost'];
let replacementWordsCount = 0;

// Second, cycle through overused words while accounting for the first instance of each overused word that's not going to be replaced
overusedWords.forEach(word => {
  let count = 0;
  for (let i = 0; i < betterWords.length; i++) {
    if (betterWords[i] === word) {
      count++;
      if (count > 1) {
        replacementWordsCount++;
        betterWords.splice(i, 1, replacementWords[replacementWordsCount-1]);
      }
    }
  };
});
1 Like

This was really helpful, thank you for sharing.

Hi coding mates !

I have (like a lot of you) been thinking a lot on those three challenges.
Here are my solutions for the two first of those.

/------- Challenge 1 ----/
/* Let’s first declare the num%2 equation and the counting variables for each overused word */
const even=num=>num%2===0;
let veryCount=0;
let reallyCount=0;
let basicallyCount=0;

/lets then declare our new variable, where everything happens/
const evenBetterWords=betterWords.filter(wordy=>{
if(overusedWords.includes(wordy)===false){
return wordy /return every word that is not overused to the variable/
}else if (wordy===‘very’){
veryCount+=1;
if(even(veryCount)){
return wordy; /* Here, if a word is recognized as overused, it is added as a +1 to the count of that word. Thanks to the function “even” we can check if that count is dividable by 2. If this comes out as true, then the overused ord passes the test and is sent to the evenBetter variable. We then do the same for the two other overused Words.*/
}
}else if (wordy=‘really’){
reallyCount+=1;
if(even(reallyCount)){
return wordy;
}
}else if (wordy=‘basically’){
basicallyCount++;
if(even(basicallyCount)){
return wordy;
}
}
})

/----- Challenge 2 -----/
This one took me a while, had to read through that topic a few time before I start to understand the logic here. So if you find yourself in a hard position, not finding the solution, do not quit ! Keep going at it, read the documentation, read your notes, go for a break, you can do it ! And once you are done, you can go for a victory dance (I did.).
I found the answer codes in the topic a little complex and thought I could find something more clear, with only what we have learn so far.
BTW, I didn’t bother changing the elements to Lowercase or get rid of the punctuation. I was more interested in what’s here below… But feel free to do it !
Here it is :

/Let’s first declare a function that counts the appearence of a word in an array/
function countWord(array, word){
let wordCount=0;
for(let i=0; i<array.length; i++){
if(array[i]===word){
wordCount++;
}
}
return wordCount
}

/Then we will use .forEach to go through our function countWord.
For that, we need those to variables “max” and “wordMax”. So each time a word recurrence is greater than the previous ones those two variables will be updated.
/
let max=0;
let wordMax=’’;

/Then we declare the function that will update those variables/
function maxWordCount(element){
let power=countWord(betterWords, element); /*We declare the variable “power” so the result of countWord goes somewhere */
if(power>max){
max=power;
wordMax= element;
}
}

betterWords.forEach(maxWordCount); /let’s declare this .forEach iterator finally/

console.log(The most common word is ${wordMax}. It appears ${max} times.);

Didn’t go for the third challenge yet. But I will !
Go luck to you all, and keep on with the good work

2 Likes

Also, try to think of it like: may this be actually easy? In meaning of far less code as you might think, especially if you’re new to coding.
Many times It might feel like a curse before it becomes a blessing.
A challenge of it’s own to be honest

8.2:

const findMostWord = (arr) => {
  let temp = 1,
  counter = 0,
  element;  
  for (let i=0; i < arr.length; i++) {
    for (let j=i; j < arr.length; j++) {
      if (arr[i] == arr[j]) {
      counter++;
      }
      if (temp < counter){
        temp = counter; 
        element = arr[i];
      }
    }
    counter=0;
  }
  console.log(`"${element}" apperas the most - total of ${temp}`);
}

findMostWord(betterWords);
1 Like

I think this code doesn’t account for ’ " ’ that is used in “The 9W to Nyack”. So the counter for ‘the’ should be wrong, right?

heyyy i was stuck at 8-2nd. and your answer works!!!thank you for your explanation :pray:t2: