JavaScript Run-length encoding

created 2021/10/22updated 2021/10/22
js
const getLengthOfOptiomalCompression = function(str) { if (str.length === 0) { console.log('Please enter valid string.'); return; } let output = ''; let count = ''; for (let i = 0; i < str.length; i++) { count++; if (str[i] !== str[i+1]) { output += str[i] + count; count = 0; } } console.log(output); } getLengthOfOptiomalCompression('aaaa'); getLengthOfOptiomalCompression('aaaabbccddd');