Return the first word with the greatest number of repeated letters

前端 未结 5 1202
慢半拍i
慢半拍i 2021-01-16 23:16

This is a question from coderbyte’s easy set. Many people asked about it already, but I’m really curious about what’s wrong with my particular solution (I know it’s a pretty

5条回答
  •  渐次进展
    2021-01-16 23:56

    Please find below the workable version of your code:

    function LetterCountI(str) {
        str = str.toLowerCase();
        var arr = str.split(" ");
        var count = 0;
        var word = "-1";
        for (var i = 0; i < arr.length; i++) {
            for (var a = 0; a < arr[i].length; a++) {
                var countNew = 0;
                for (var b = a + 1; b < arr[i].length; b++) {
                    if (arr[i][a] === arr[i][b])
                        countNew += 1;
                }
                if (countNew > count) {
                    count = countNew;
                    word = arr[i];
                }
            }
        }
        return word;
    }
    

提交回复
热议问题