Adding new key-value in Object and looping through object

input = ravi ranjan kumar
var name = { }

Now I want my object to look like
name = {r : 2,
a : 4,
v : 1,
i : 1, … }

I am unable to understand

  1. how can I push each key value by itterating through my input string ?
  2. after getting the desired above object how can I get a result printed like a =4, v =1 … like that.

Please help…

One approach would be to iterate through each character of the string and check if your name object contains the character as a key. If it does not, then you set the key’s value to 1, otherwise just add 1 to the existing key’s value.

See if you can implement this strategy.

Note: The above strategy would count the space character as a key also.

To iterate through the characters of a string you could do something like

for (let i = 0; i < input.length; i++) {
  char = input[i];
}

Outside this loop you could count for different times a letter appears by using a further loop that could check for each letter individually, this could help create your desired object . Hope I understood your question and this is useful