I want to find how many times a character is given in string.
Python codes works
def func(a):
d={}
for ch in a:
if ch in d:
d[ch] += 1
else:
d[ch] = 1
print d
and javascript
function how_many_times(string) {
object = {};
for (var i in string) {
if (i in object) {
object[i] += 1
} else {
object[i] = 1
}
}
console.log(object)
}
how_many_times('Hello, World !')
When I try to pass this to javascript, I don't get the same result. I tried like this because I read python dictionary and javascript object are very similar. I know I can solve problem with other ways but I want to learn why it doesn't work in objects.