javascript Runtime Error Message: Line 12: TypeError: Cannot read property 'length' of undefined


  • 0
    C

    I use javascript

    /**
     * @param {string} s
     * @return {number}
     */
    var lengthOfLastWord = function(s) {
        if(s == ""){
            return 0;
        }
        var sArr = s.split(" ").filter(function(n){return n});
        var LastWord = sArr[sArr.length -1];
        return LastWord.length;
    };
    

    please tell me why


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.