I have to display the word count next to each word.
let str = "Hello there, how are you? Can you tell me how to get to the nearest Starbucks?"
function countingWords(){
if (str.length === 0){
return null;
}
str = str.toLocaleLowerCase();
str.split(" ").forEach(word => {
console.log(word, "=", str.split(word).length-1,)
});
return
}
countingWords()
My output:
hello = 1
there, = 1
how = 2
are = 2
you? = 1
can = 1
you = 2
tell = 1
me = 1
how = 2
to = 2
get = 1
to = 2
the = 2
nearest = 1
starbucks? = 1
The majority of this is correct but I still get a few wrong answers like "are=2" and "the=2". Does anyone know why or how to fix?