Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Saw the Stick by kurosawa4434
"use strict";
function stick(data) {
let tn = [1];
let n = 1;
while (tn.length) {
const tn_sum = tn.reduce((a, b)=>a+b);
if (data === tn_sum) {
break;
} else if (data < tn_sum) {
tn.shift();
} else if (data > tn_sum) {
n += 1;
tn.push(tn[tn.length-1]+n);
}
}
return tn;
}
var assert = require('assert');
if (!global.is_checking) {
// These "asserts" are used for self-checking and not for an auto-testing
assert.deepEqual(stick(64), [15, 21, 28])
assert.deepEqual(stick(371), [36, 45, 55, 66, 78, 91])
assert.deepEqual(stick(225), [105, 120])
assert.deepEqual(stick(882), [])
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
Aug. 9, 2018
Comments: