Game of Three - While Solution

by jacobwsmith

JavaScript

'use strict';

/*
 * Basic while loop solution
 * @param {Number}
 */
function solution1(input) {
  var arr = [],
    adjust;
  while (input > 1) {
    adjust = incrementBy(input);
    arr.push(input + ' ' + adjust);
    input = (input + adjust) / 3;
  }
  arr.push('1')
  return arr;
}

/*
 * Helper method that returns the increment value
 * @param {Number}
 */
function incrementBy(n) {
  switch (n % 3) {
    case 1:
      return -1;
    case 2:
      return 1;
    case 0:
      return 0;
  }
}

///////

// tests
console.log(solution1(100));