Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
ceil and slice solution in Clear category for Split List by Joni_Braslaver
import assert from "assert";
function splitList(values: number[]) {
const middle = Math.ceil(values.length / 2)
return [values.slice(0,middle), values.slice(middle, values.length)];
}
console.log('Example:');
console.log(splitList([1, 2, 3, 4, 5, 6]));
// These "asserts" are used for self-checking
assert.deepEqual(splitList([1, 2, 3, 4, 5, 6]), [[1, 2, 3], [4, 5, 6]]);
assert.deepEqual(splitList([1, 2, 3]), [[1, 2], [3]]);
assert.deepEqual(splitList([1, 2, 3, 4, 5]), [[1, 2, 3], [4, 5]]);
assert.deepEqual(splitList([1]), [[1], []]);
assert.deepEqual(splitList([]), [[], []]);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
April 24, 2020