I am taking input from the user. I have that bit done. The input can be a word or even a sentence saved as a string. What I want to do is then count the number of times the letters appear in the input, and also have it sorted alphabetically.
Example input:
learning to program
Example output:
a 2
e 1
g 2
i 1
How to count occurences in a string is explained here: https://stackoverflow.com/a/881111/8935250 I tried to mimic your example output in this code fiddle.
const string = "learning to program"
function count(character) {
return string.split(character).length
}
map = string.split("").map(c => {
return {c, count: count(c)}
})
map.sort((a,b) => b.count - a.count)
console.log(map)
console.log(string.split("").sort((a,b) => string.split(b).length - string.split(a).length))
should also do the job but does not show the occurrences.