Counting words from stored .md files The following searches recursively for all the mark down files - i. ending with the extension .md - inside a folder. It then stores the text of the files in an array. Finally, it sums their word count using a wordCount() function (from the compromise package).
import nlp from 'compromise'
import fs from 'fs'
import path from 'path'

const filePaths = searchRecursive(`${__dirname}/test`, '.md')

const fileTexts = filePaths.map((filePath: string) =&gt; {
    return fs.readFileSync(filePath, 'utf-8')
}) // -&gt; ['one two', 'one two three']

let totalWordCount = 0

fileTexts.forEach((fileText: string) =&gt; {
    totalWordCount += nlp(fileText).wordCount()
})

console.log(totalWordCount) // -&gt; 5

function searchRecursive (dir: string, pattern: string) {
    ...
}

I've heard that forEach shouldn't be used in functional programming. How can I modify this code so it doesn't use forEach?
How to modify this code, so it doesn't use forEach?

One way to achieve this is using Array.prototype.reduce():
const totalWordCount = fileTexts.reduce((big: sum, fileText: string) =&gt; {
    return sum + nlp(fileText).wordCount()
}, 0)

Notice that totalWordCount is assigned once so const can be used which helps avoid accidental re-assignment and other bugs.
If fileTexts is only used to calculate totalWordCount then the two loops could be combined into a single loop (e.g. with reduce()).
To learn more about functional techniques try these functional exercises.