Code Interview – Coderbyte – Run Length – Code challenge – JavaScript Solution Source Code

Coderbyte

Run Length

Have the function RunLength(str) take the str parameter being passed and return a compressed version of the string using the Run-length encoding algorithm. This algorithm works by taking the occurrence of each repeating character and outputting that number along with a single character of the repeating sequence. For example: “wwwggopp” would return 3w2g1o2p. The string will not contain any numbers, punctuation, or symbols.

function RunLength(str) { 

let result = "";
let count = 1;
  
  for (let i = 0; i < str.length; i++) {
    if (str[i] === str[i + 1]) { // If the current character is the same as the next one, increase the count
      count++;
    } else { // If the current character is different from the next one, add the count and the character to the result
      result += count + str[i];
      count = 1; // Reset the count
    }
  }
  
  return result;

}
   
// keep this function call here 
console.log(RunLength(readline()));

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *