Skip to content

bfamchon/tdd-talk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Conway

STEP 0:

  expect(draw("1")).toBe("1\n11");
  const draw = line => {
      return "1\n11";
  };

STEP 1:

  expectConwaySuite("2", "2", "12");
  const draw = line => {
      return line + "\n" + line.length + line.charAt(0);
  };

STEP 2:

  expectConwaySuite("22", "22", "22");
  const drawSourceLine = (line) => {
    return line + LINE_JUMP;
  }

  const drawNextLine = (line) => {
    return line.length + line.charAt(0);
  }

  const draw = line => {
      return drawSourceLine(line) + drawNextLine(line);
  };

STEP 3:

  expectConwaySuite("21", "21", "1211");
const countConsecutiveNumbers = line => {
    return line.length + line.charAt(0);
}

const drawNextLine = (line) => {
    if (line.length > 1 && line.charAt(0) !== line.charAt(1)) {
        return countConsecutiveNumbers("2") + countConsecutiveNumbers("1");
    }
    return countConsecutiveNumbers(line);
}

STEP 4:

  expectConwaySuite("213", "213", "121113");
const drawLineChunks = (line, chunks = "") => {
    if (line.length > 1 && line.charAt(0) !== line.charAt(1)) {
        return drawLineChunks(
            line.substring(1),
            chunks + countConsecutiveNumbers(line.substring(0, 1))
        );
    }
    return chunks + countConsecutiveNumbers(line);
};

const drawNextLine = (line) => {
    return drawLineChunks(line);
}

STEP 5:

  expectConwaySuite("2331", "2331", "122312");
const findIndexOfNextDistinctNumber = (line, index) => {
    if (line.length === 1 || line.charAt(0) !== line.charAt(1)) return index;
    return findIndexOfNextDistinctNumber(line.substring(1), index + 1);
}

const drawLineChunks = (line, chunks = "") => {
    if (line.length === 0) {
        return chunks;
    }
    const indexOfNextDistinctNumber = findIndexOfNextDistinctNumber(line, 0);
    return drawLineChunks(
        line.substring(indexOfNextDistinctNumber + 1),
        chunks + countConsecutiveNumbers(line.substring(0, indexOfNextDistinctNumber + 1))
    );
}

STEP 6:

  expectConwaySuite("1", 3, "1", "11", "21", "1211");
const drawSuite = (line, deep, conwaySuite = "") => {
    if (deep === 0) {
        return conwaySuite;
    }
    const nextLine = drawNextLine(line);
    return drawSuite(nextLine, deep - 1, conwaySuite ? conwaySuite + LINE_JUMP + nextLine : nextLine);
}

const draw = (line, deep) => {
    return drawSourceLine(line) + drawSuite(line, deep);
};

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published