I counted the frequency of each character, but what do I do now?
As I understand the formula chould be
(char_count/length)*log(2, (char_count/length))
for each character in the object, anyone please clarify what the formula is? i tried to look it up but rather than the whole topic of information theory or code that i still don’t really understand I didn’t find much
Your code so far
function entropy(str) {
var chars = {}, numChars = 0, len = str.length,
res = 0, tmp;
for(let i of str) {
if(chars[i]) chars[i]++;
else {chars[i] = 1; numChars++;}
}
for(let i in chars) {
tmp = chars[i]/len;
res += tmp;
//Math.log(2,tmp), what do you do with this?
}
return res;
}
console.log(entropy("0123"))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36
.
Challenge: Entropy
Link to the challenge: