Answers for "Solved - TypeError: Cannot read properties of null (reading 'length')"

0

Solved - TypeError: Cannot read properties of null (reading 'length')

//A function that counts the occurrence of an specific word from string using regex object
function wordCount(w,arr) {
    let sum = 0
    let re = new RegExp(w, 'ig')
    for (let i = 0; i < arr.length; i++) {
         //The match() retures null if the word is not in the string
        //.lenght reture array lenght
        //If word not fount add 0 Eles add the count 
        sum += (!arr[i].match(re))? 0 : arr[i].match(re).length
    }

    return sum
}
Posted by: Guest on April-01-2022

Code answers related to "Solved - TypeError: Cannot read properties of null (reading 'length')"

Code answers related to "Javascript"

Browse Popular Code Answers by Language