JSFiddle - React, Tailwind, and code Playground

by Balázs Baumgartner

JavaScript

function StringChallenge(str) { 
  let charCount = 0;
  let compressed = '';

  for (let i = 0; i < str.length; i += 1) {
    charCount += 1;

    if (str[i] !== str[i + 1]) {
      compressed = `${compressed}${charCount}${str[i]}`;
      charCount = 0;
    }
  }

  return compressed; 
}

const reverseCompressed = (str) => {
	let originalStr = '';
  
  for (let i = 0; i < str.length; i += 2) {
	  const currentChars = [...Array(parseInt(str[i], 10)).keys()].map(() => (str[i + 1]));
    originalStr = `${originalStr}${currentChars.join('')}`;
  }
  
	return originalStr;
}

// "TESTS"
// console.log(StringChallenge('aabbcde') === '2a2b1c1d1e');
// console.log(StringChallenge('wwwbbbw') === '3w3b1w');
console.log(reverseCompressed('2a2b1c1d1e'));