Avatar of brunnock

brunnock's solution

to Run Length Encoding in the JavaScript Track

Published at Nov 20 2018 · 0 comments
Test suite


This exercise has changed since this solution was written.

Implement run-length encoding and decoding.

Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count.

For example we can represent the original 53 characters with only 13.


RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.


For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace. This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character.


Go through the setup instructions for JavaScript to install the necessary dependencies:


Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine run-length-encoding.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.


Wikipedia https://en.wikipedia.org/wiki/Run-length_encoding

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


var RLE = require('./run-length-encoding');

describe('Run-length encoding', function () {
  it('encode empty string', function () {

  xit('encode single characters only', function () {

  xit('decode empty string', function () {

  xit('decode single characters only', function () {

  xit('encode simple', function () {

  xit('decode simple', function () {

  xit('encode with single values', function () {

  xit('decode with single values', function () {

  xit('decode(encode(...))combination', function () {
    expect(RLE.decode(RLE.encode('zzz ZZ  zZ'))).toEqual('zzz ZZ  zZ');
const RLE = {}

RLE.encode = function (str) {
  let strarr = str.split('');
  let num=1;
  let char=strarr[0];
  let code='';
  for (let x=1; x<=strarr.length; ++x) {
    if (strarr[x] === char) {
    } else {
      if (num>1) code += num;
      code += char;
      char = strarr[x];

const numCharExp = /(\d+\D)/;

RLE.decode = function (str) {
  return (str.split(numCharExp).map(x=>{
    if (numCharExp.test(x)) {
      let term = x.split(/(\d+)/);
      return (term[2].repeat(term[1]));
    } else {

module.exports = RLE;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?