Javascript Runtime error


  • 0
    Y
    var groupStrings = function(strings) {
        var letters = 'abcdefghijklmnopqrstuvwxyz';
        var buildLetterMap = function() {
            var map = {}, i = 0;
            for (; i < 26; i += 1) {
                map[letters[i]] = i;
            }
            return map;
        };
        var letterMap = buildLetterMap();
        var convertToBase = function(string) {
            var n = string.length;
            var base = '';
            var i = 0, letter;
            var gap = n > 0 ? (letterMap[string[0]] - letterMap.a) : 0;
            if (gap > 0) {
                for (; i < n; i += 1) {
                    letter = string[i];
                    base += letters[(letterMap[letter]-gap+26)%26];
                }
            }
            return base;
        };
        var i, string, baseString;
        var result = [], map = {}, n = strings.length;
        Array.sort(strings);
        for (i = 0; i < n; i += 1) {
            string = strings[i];
            baseString = convertToBase(string);
            if (map[baseString]) {
                result[map[baseString]].push(string);
            } else {
                result.push([string]);
                map[baseString] = result.length-1;
            }
        }
        return result;
    };
    

    I got error: Runtime Error Message: Line 30: TypeError: undefined is not a function.Last executed input: []. Line 30 is the line "Array.sort(strings);".

    If i run it in a browser, it runs fine without error.

    Any help is appreciated.


  • 0

    I don't know why it works in your browser, what browser are you using? Anyway, try the normal way to sort:

    strings.sort();

  • 0
    Y

    thank you! I don't know why there's no console error under Firebug.


  • 0

    Looks like it's one of many non-standard JavaScript extensions by Firefox's JavaScript engine.


Log in to reply
 

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