counting frequency of characters in a string using javascript -
i need write kind of loop can count frequency of each letter in string.
for example: "aabsssd"
output: a:2, b:1, s:3, d:1
also want map same character property name in object. idea how this?
i not sure how it.
this far:
var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; function counter(x) { var count=0, temp = []; x = x.split(''); console.log(x); for(var i=0, len = x.length; < len; i++) { if(x[i] == "a") { count++; } } return count; } var = "aabbddd"; console.log(counter(a));
here go:
function getfrequency(string) { var freq = {}; (var i=0; i<string.length;i++) { var character = string.charat(i); if (freq[character]) { freq[character]++; } else { freq[character] = 1; } } return freq; };
Comments
Post a Comment