Javascript text compression

🧩 Syntax:
const numToBin = num => num.toString(2);
const binToNum = bin => parseInt(bin,2);

function buildTree(freqs) {
	let tree = [];
	let sorted = [];
	let check = 1;
	// sort chars by frequency
	while (sorted.length != Object.keys(freqs).length) {
		for (const i in freqs) {
			if (freqs[i] == check) {
				sorted.push(i);
			}
		}
		check++;
	}
	// sorted.reverse();
	tree = sorted.map((v) => {
		return [v, freqs[v]]
	});
	// console.log(sorted);
	
	// create tree
	while (tree.length > 1) {
		let newElem = [[tree[0][0],tree[1][0]], tree[0][1]+tree[1][1]];
		
		tree = tree.slice(2);
		let slot = 0;
		while (slot < tree.length && tree[slot+1][1] <= newElem[1]) {
			slot++;
		}
		tree.splice(slot+1, 0, newElem);
	}
	
	let newTree = [];
	
	return tree;
}

function assignKeys(tree) {
	let keys = [];
}

function compressText(txt) {
	let chars = [];
	let freqs = [];
	for (let i = 0; i < txt.length; i++) {
		let code = txt[i].charCodeAt(0);
		
		if (freqs[txt[i]] == undefined) {
			freqs[txt[i]] = 1;
		} else {
			freqs[txt[i]]++;
		}
		
		chars.push(code);
	}
	
	console.log(buildTree(freqs));
}
compressText("The FitnessGram Pacer Test is a multistage aerobic capacity test that progressively gets more difficult as it continues. The 20 meter pacer test will begin in 30 seconds. Line up at the start. The running speed starts slowly, but gets faster each minute after you hear this signal. [beep] A single lap should be completed each time you hear this sound. [ding] Remember to run in a straight line, and run as long as possible. The second time you fail to complete a lap before the sound, your test is over. The test will begin on the word start. On your mark, get ready, start.");

function decompressText(data) {}