Minilinter , function to get the amount of time an overusedword appears is not evaluating if conditions

Hello Everyone , so in the Minilinter project we are meant to get the amount of time an overused word is used . basically we are to ouptut a particular overused word and the number of times it was used . i wrote a function to handle that, but a particular if condition in the function is not even evaluated at all. i used VSC debugger and i can see that the local variables are created and the if condition code is run , yet it seems to have no effect . here is the function .

const overUsedWordCount =(story,overUsed) =>{
    let words =story.split(" ")
    console.log(words)
    
    let map ={}
    for(let i=0; i<words.length; i++){
        if(map[words[i]]){
            map[words[i]]++
            continue
        }
        map[words[i]]=1
    }
    const prepare= ()=>{
        console.log(map)
        for (let [key,value] of Object.entries(map)){
            if(overUsed.indexOf(key > -1 )){
                console.log(key + " appeared "+ value +" times")
            }
        }
    }
    prepare()
   //console.log(map)
}

For clearer overview , here is the full source code of my project .

//does not require that much of user input , not a cool project but 
// i'll do it anyway

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ['really', 'very', 'basically'];

let unnecessaryWords = ['extremely', 'literally', 'actually' ];

const getWords = ()=>{
    let storyWords = story.split(" ")
    
    return storyWords.length
}

const removeUncessaryWords =(wordArray)=>{
    wordArray = wor
    let betterWords = wordArray.filter((word)=>{
       return unnecessaryWords.indexOf(word)  <1
        
    })
    return betterWords
}
const getoverUsedWords = (wordArray)=>{
    let wordcount = wordArray.filter((word)=>{
        return overusedWords.indexOf(word)> -1
    }).length

    return wordcount.length;
    
}
const getSentenceCount=(wordArray)=>{
    wordArray = wordArray.split("")
       let sentenceCount =0
       wordArray= wordArray.forEach((word)=>{
           if(word[word.length-1]==="." || word[word.length-1] ==='!'){
               sentenceCount++
           }
       })
       return sentenceCount
    
}

const overUsedWordCount =(story,overUsed) =>{
    let words =story.split(" ")
    console.log(words)
    
    let map ={}
    for(let i=0; i<words.length; i++){
        if(map[words[i]]){
            map[words[i]]++
            continue
        }
        map[words[i]]=1
    }
    const prepare= ()=>{
        console.log(map)
        for (let [key,value] of Object.entries(map)){
            if(overUsed.indexOf(key > -1 )){
                console.log(key + " appeared "+ value +" times")
            }
        }
    }
    prepare()
   //console.log(map)
}
const outpuEverything=()=>{
    console.log(`the total number of words is ${getWords()}`)
    console.log(`The number of sentence is ${getSentenceCount(story)}`)
    overUsedWordCount(story,overusedWords)
    
}
outpuEverything()
console.log(overusedWords)

why is the if condition in the prepare function embeded inside overusedwordcount function not evaluated. it is suppose to check if a particular key in the map is also present in the overusedwords array and if it is , it should output the key and value since the key is word and the value is the number of times the word appears.

Hi, welcome to the forums!

According to the docs, the indexOf()method returns -1 if the given value is not in the array. The if-statement seems to be evaluating -1 as greater than -1 (I also tried replacing > with !==, same result.) Frankly I don’t know what’s going on here - maybe someone with deeper experience of JavaScript can chime in?

The purpose of this if-statement is to check whether each key exists in the overUsed array. A more straightforward way to check for inclusion in an array is the .includes() array method. Using it in the if-statement results in the correct output:

the total number of words is 188
The number of sentence is 12
// (array of words here)
really appeared 2 times
basically appeared 1 times
very appeared 5 times

.indexOf() appears to be the problem, but for our purposes here we don’t need it.

1 Like

I think the best way to do it is the one suggested by @chrysippa :

You can also use ìndexOf()`, but it returns the index of your key in the array rather than a boolean value. Therefore the correct way to use it would be this:

if(overUsed.indexOf(key) > -1) {
                console.log(key + " appeared "+ value +" times");
            }

Thanks alot , i never noticed i wrapped them both in the same bracket. thanks .

Ah, thanks for pointing that out! It’s always the little things.