Game of Three - Recursive
by jacobwsmith
JavaScript
'use strict';
/*
* Recursion solution
* @param {Number}
* @param {Array} Optional
*/
function solution2(input, arr = []) {
if (input <= 1) {
arr.push('1');
return arr;
}
var adjust = incrementBy(input);
arr.push(input + ' ' + (adjust.toString()));
return solution2((input + adjust) / 3, 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(solution2(100));