JSFiddle - React, Tailwind, and code Playground

by Jessie Lau

JavaScript

function LetterCountI(str) {
    var words = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g, "").split(" ");
    var longestWord;
    var maxFound = 0;
    for (var i = 0; i < words.length; i++) {
        var currentWord = words[i];
        var maxNumber = maxLetter(currentWord);
        if (maxNumber > maxFound) {
            maxFound = maxNumber;
            longestWord = currentWord;
        }
    }
    if (maxNumber === 1) {
        return -1;
    }
    return longestWord;

}

function maxLetter(word) {
    var charCounter = {};
    var maxNumber = 0;
    for (var i = 0; i < word.length; i++) {
        var currentChar = word[i];
        if (charCounter[currentChar]) {
            charCounter[currentChar]++;
        } else {
            charCounter[currentChar] = 1;
        }

        if (maxNumber < charCounter[currentChar]) {
            maxNumber = charCounter[currentChar];
        }

    }
    return maxNumber;
}

console.log(LetterCountI("Today,......... is the greatest day ever!"));
console.log(LetterCountI("dog dog cat"));
console.log(LetterCountI("how are you"));