This might be a basic question, however, I was struggling a little. So, what the code does is that it traverses an entire string passed to it and maps the occurrence of each letter. For instance, {P:28, A:15 ....}. What I wanted to know was that if I want to access the value, 28 in the case of P, and perform some sort of action on it, 28/(length of message) * 100, how can I do that and replace the value in the map. I would really appreciate it if someone can share it. Thanks.
P.S: I tried updating at every instance, but I need to only update once the for loop ends to get the correct percentage of occurrence.
const frequency = (message) => {
message = message.toUpperCase().replace(/\s/g,'');
for(let i = 0; i < message.length; i++)
{
let letter = message.charAt(i);
if(map.has(letter)){
map.set(letter, map.get(letter) + 1);
}
else{
map.set(letter, (count));
}
}
// const sortedMap = new Map([...map.entries()].sort());
return map;
}