Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
slice solution in Clear category for Split Pairs by Sim0000
import assert from "assert";
function splitPairs(text: string): string[] {
let result: string[] = [];
for(let i: number = 0; i < text.length; i += 2){
result.push((text + '_').slice(i, i + 2));
}
return result;
}
console.log('Example:');
console.log(splitPairs('abcd'));
// These "asserts" are used for self-checking
assert.deepEqual(splitPairs('abcd'), ['ab', 'cd']);
assert.deepEqual(splitPairs('abc'), ['ab', 'c_']);
assert.deepEqual(splitPairs('abcdf'), ['ab', 'cd', 'f_']);
assert.deepEqual(splitPairs('a'), ['a_']);
assert.deepEqual(splitPairs(''), []);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
April 24, 2020
Comments: