Hi all,
I’m trying to solve step 3 of the mini-linter project by using a nested loop:
https://www.codecademy.com/courses/introduction-to-javascript/projects/mini-linter
Here’s my code:
for (let i = 0; i < betterWords.length; i++){
for (let j = 0; j < overusedWords.length; j++){
if (betterWords[i] === overusedWords[j]){
if (betterWords[i] === "really"){
return reallyCount+= }
else if (betterWords[i] === "very"){
return veryCount+= }
else if (betterWords[i] === "basically"){
return basicallyCount+= }
else resultArray.push(betterWords[i])}
}
}
I’m running into syntax errors that I can’t move past. Please could somebody step in and let me know what I’m doing wrong?
Cheers!
I realised that I wasn’t instructing how much to add to the counts, so I changed to the below:
for (let i = 0; i < betterWords.length; i++){
for (let j = 0; j < overusedWords.length; j++){
if (betterWords[i] === overusedWords[j]){
if (betterWords[i] === "really"){
return reallyCount+= 1}
else if (betterWords[i] === "very"){
return veryCount+= 1}
else if (betterWords[i] === "basically"){
return basicallyCount+= 1}
else resultArray.push(betterWords[i])}
}
}
Now I just get a blank console when I run my code - even when trying to log the counts and arrays.
The demo uses a different method - would love to be hand-held through this method for my own knowledge.
Cheers!
Hi design4779428521, I’m assuming by looking at your code, you’re talking about step 4, not 3. Two things I noticed in your code:
return reallyCount+= 1
return veryCount+= 1
return basicallyCount+= 1
A return statement is unnecessary because you’re not inside a function. Even if you were inside one, a return statement means the rest of the code won’t be executed, and won’t work properly when you’re inside a loop.
It’s better to omit the return
and just use the statement after the loop (While inside a function).
In this case just omitting the return
should be enough.
else resultArray.push(betterWords[i])}
Also, if I’m not mistaken, your else
statement is compared with the inner if
statement and not the outer.
Meaning it’s inside the outer if
.
Thanks - sorry I took so long to get back to this, real life got in the way as it so often did.
I ended up with the following which seems to work fine:
for (let i = 0; i < betterWords.length; i++){
for (let j = 0; j < overusedWords.length; j++){
if (betterWords[i] === overusedWords[j]) {
if (betterWords[i] === 'really') {
reallyCount+= 1 }
else if (betterWords[i] === 'very') {
veryCount+= 1 }
else if (betterWords[i] === 'basically') {
basicallyCount+= 1 }
}
}
}
Thanks once again!
1 Like
In step #4 in the Mini-Linter project, the instructions call to count how many times words in the overused array appear in the story.
I wanted to write a code that is blind the identity of the elements of the overused array (does not know that there are only 3 words:“really”, “very”, basically"), but that would work of an array we perhaps have no access to, or may be has many words on it, in which case it would be pointless to compare the named elements (one by one) with the tested text (as the video solution and the hint suggest). I tried to use .map() and a for loop. I failed to do that.
How can it be achieved?
Here is the code I wrote:
const indeedOverused = betterWords.map(word => {
for (let i = 0; i < overusedWords.length; i++) {
if (overusedWords[i].includes(betterWords(word)) === true) {
return word;
console.log(The word ${overusedWords[i]} was used ${indeedOverused.length} times
);
}
}});