Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Split Pairs by s0me1
import assert from "assert";
function splitPairs(text: string): string[] {
if (text.length % 2 === 1) {
text += '_';
}
let res = [];
for (let i = 0; i < text.length; i += 2) {
res.push(text.slice(i, i + 2));
}
return res;
}
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!");
June 26, 2020