Created
October 25, 2019 23:37
-
-
Save d0peCode/d6e7730c11a316c60bd5f7c0006124f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class charactersCounter { | |
constructor(input) { | |
this.inputArr = input.split(''); | |
this.availableLetters = [...new Set(this.inputArr)]; | |
this.mostPopular = { | |
letter: '', | |
amount: 0 | |
} | |
} | |
getOccurrence(arr, val) { | |
return arr.filter(v => (v === val)).length; | |
} | |
addLetterAmount(targetArray) { | |
for(let i = 0, l = targetArray.length; i < l; i++) { | |
targetArray[i] = { | |
letter: targetArray[i], | |
amount: this.getOccurrence(this.inputArr, targetArray[i]) | |
} | |
} | |
} | |
getMostPopular(fromArray) { | |
for(let i = 0, l = fromArray.length; i < l; i++) { | |
if(fromArray[i].amount > this.mostPopular.amount) { | |
console.log(fromArray[i].amount, this.mostPopular) | |
this.mostPopular.letter = fromArray[i].letter; | |
this.mostPopular.amount = fromArray[i].amount; | |
} | |
} | |
} | |
init() { | |
this.addLetterAmount(this.availableLetters); | |
this.getMostPopular(this.availableLetters); | |
return this.mostPopular.letter; | |
} | |
} | |
const charCounter = new charactersCounter('hello worlddd'); | |
console.log(charCounter.init()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment