The steps you need the function to take:
- separate the string into pieces
- create an object
- assigns the object a property for each letter
- count how many occurrences
This function could be like this (put optional count or not with large/small letters, by default not counting):
function charCount(str, keepCase) {
if (!keepCase) str = str.toLowerCase();
var obj = {};
for (var i = 0; i < str.length; i++) {
if (!obj[str[i]]) obj[str[i]] = 0;
obj[str[i]]++;
}
return obj;
}
and testing would be:
var a = charCount("hello");
var b = charCount("AaBbC");
var c = charCount("AaBbC", true); // contando com letras grandes!
console.log(a, b); // Object {h: 1, e: 1, l: 2, o: 1} Object {a: 2, b: 2, c: 1}
console.log(c); // Object {A: 1, a: 1, B: 1, b: 1, C: 1}
What have you been able to do so far? Edit the question and enter the code so we can help you.
– Zignd