Mini Linter Refactoring

The body of my code works. I am trying to refactor and include the logic in a function. When I console log the function I get undefined and sentenceCount remains at 0. I am assuming it has something to do with scope, but have not been able to resolve this. Any help understanding would be greatly appreciated
let sentenceCount = 0
function sentences(){
for (word of storyWords) {
if(word[word.length -1] === ‘.’ || word[word.length -1] === ‘!’){
sentenceCount += 1
}
}
}

To insulate the actual count/accumulator variable, we would declare it inside the function, then return its accumulated value in the end. The caller can assign it to any variable at that point, or simply log the value. A running global is not a preferred variable in this instance. Each call to the function will give us an updated value so we don’t need to poll the global, hence it’s not being necessary.

1 Like