JSFiddle - React, Tailwind, and code Playground

JavaScript

const input = 'aabcccccaaa'
const output = 'a2b1c5a3'

expect(compress(input), output)

/**
 * Implement a method to perform basic string comparision using
 * the counts of repeated characters. For example, the string aabcccccaaa
 * would become a2b1c5a3. If the "compressed" string would not become smaller
 * than the original string, your method should return the original string.
 * You can assume that the string has only uppercase and lowercase letters (a-z).
 */
function compress(str) {
	  let retval = '';
    let prevLetter = str[0];
    let counter = 0;
    for (let i = 0; i < str.length; i++) {
        if (prevLetter != str[i]) {
            retval += prevLetter + counter;
            prevLetter = str[i];
            counter = 1;
        } else {
            counter++;
        }
    }
    retval += prevLetter + counter;
    return retval;
}

function expect(actial, expected) {
  document.body.innerHTML = actial === expected ?
    '<span style="color:green">Success</span>' :
    '<span style="color:red">Fail</span>'
}