CodinGame - Solution in Javascript to Conway Sequence Problem


This is a Javascript implementation of a solution to the CodinGame John Horton Conway (the "Conway Sequence") problem.

The article is the second in a series that I started with the goal of testing the feasibility of giving solutions to CodinGame challenges in Javascript. You can find the first solution posted here.

  // execute the program's main entry point

  // contains the logic to read input, process it, and generate output
  function main()
    var R = parseInt(readline());
    var L = parseInt(readline());
    var sequence = makeSequence(R, L);
    var sequenceString = printSequence(sequence);

  // generates a Conway Sequence based on an input seed number and number of iterations
  function makeSequence(seed, iteration) {

    var seq = [seed];
    // loop iteration number of items and keep building the sequence elements
    for (var i = 0; i < iteration - 1; i++) {
        var newSeq = []; var k = 1;
        var j = 1;
        while(j < seq.length) {
            if(seq[j] != seq[j-1]) {
                k = 1;
        seq = newSeq;
    return seq;

  // generates a string representation of an element in the sequence
  function printSequence(seq) {
    var str = '';
    for(var i = 0; i < seq.length; i++) {
        str += seq[i] + ' ';
    return str.trim();

The problem was pretty simple and quite straight forward to implement based on the constraints that the requested iteration would always be less than 25. Given these constraints the implementation is trivial by just following the rules of generating the sequence and running the algorithm for the specified number of iterations

The full problem statement can be found on the CodinGame Website, under the Medium difficulty puzzles section.

is the founder of Donaq, a software development consulting company with a focus on mobility. You can find Mike on Google+ and on LinkedIn.
Design copyright (c) Miky Dinescu