Python code for Huffman coding converted to Javascript