Why I got runtime error while working well on my local environment ( Javascript ) ?


  • 0
    B

    I'm solving problem "Word Ladder II" with javascript. But I encounter a runtime error telling me "Line 24: TypeError: undefined is not a function" with "Last executed input: "a", "c", ["a","b","c"]". Line 24 is here "var dictRecordLevel = dict.reduce(function(s, v){". So what it means is that "dict" variable is undefined. It's strange that input dict is ["a", "b", "c"], but it becomes undefined. After checking leetcode faq, it tells that if any static variable declared, remember to reset. But I don't see any static variable declared in my code. So, is there anyone can tell me what can I do to solve this problem ? Thanks a lot ~~~

    var isMatched = function(word1, word2){
        var diffCount = 0;
        for ( var i = 0 ; i < word1.length ; i++ ){
            if (word1[i] !== word2[i]){
                diffCount++;
                if (diffCount > 1){
    
                    return false;
                }
            }
        }
    
        return true;
    };
    
    var buildGraph = function(end, start, dict){
        var dictRecordLevel = dict.reduce(function(s, v){
            s[v] = -1;
    
            return s;
        }, {});
        var dictRecord = {};
        var queue = [end];
        dictRecordLevel[end] = 0;
        while (queue.length){
            var currentWord = queue.shift();
            dict.filter(function(wordInDict){
    
                return dictRecordLevel[wordInDict] < 0 || dictRecordLevel[wordInDict] > dictRecordLevel[currentWord];
            }).forEach(function(wordInDict){
                if (isMatched(currentWord, wordInDict)){
                    queue.push(wordInDict);
                    dictRecordLevel[wordInDict] = dictRecordLevel[currentWord] + 1;
                    if (!dictRecord[wordInDict]){
                        dictRecord[wordInDict] = [];
                    }
                    if (dictRecord[wordInDict].indexOf(currentWord) < 0){
                        dictRecord[wordInDict].push(currentWord);
                    }
                }
            });
            if (isMatched(currentWord, start)){
                if (!dictRecord[start]){
                    dictRecord[start] = [];
                }
                if (dictRecord[start].indexOf(currentWord) < 0){
                    dictRecord[start].push(currentWord);
                }
            }
        }
    
        return dictRecord;
    };
    
    var getPaths = function(start, graph){
        var paths = [];
        var stack = [[start]];
        var shortestPathLength = -1;
        while (stack.length){
            var path = stack.pop();
            if (graph[path.slice(-1)[0]] && (( shortestPathLength < 0 ) || ( shortestPathLength > 0 && path.length < shortestPathLength ))){
                graph[path.slice(-1)[0]].forEach(function(child){
                    var tmpPath = path.slice();
                    tmpPath.push(child);
                    stack.push(tmpPath);
                });
            }else{
                paths.push(path);
                shortestPathLength = path.length;
            }
        }
    
        return paths.filter(function(path){
    
            return path.length <= shortestPathLength;
        });
    };
    
    var findLadders = function(start, end, dict){
        var graph = buildGraph(end, start, dict);
        var paths = getPaths(start, graph);
    
        return paths;
    };

Log in to reply
 

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