I am trying to rework this function, so it returns more than one value. As of now, it only returns the first one, so if I have a sentence with the words "Hello to all from Boston", it will only return Hello, I am looking to reword this function so, it returns ["Hello", "all", "Boston"].
By the way, I got this solution from this previous thread.
function returnFirstRepeatChar2(str){
return ((str = str.split(' ').map(function(word){
var letters = word.split('').reduce(function(map, letter){
map[letter] = map.hasOwnProperty(letter) ? map[letter] + 1 : 1;
return map;
}, {}); // map of letter to number of occurrence in the word.
return {
word: word,
count: Object.keys(letters).filter(function(letter){
return letters[letter] > 1;
}).length // number of repeated letters
};
}).sort(function(a, b){
return b.count - a.count;
}).shift()) && str.count && str.word) || -1; //return first word with maximum repeated letters or -1
}
console.log(returnFirstRepeatChar2("Hello and hello again"));
And here is a bin. By the way this is just one of the solutions of the original thread, not sure if its the best performing one.