Skip to content

Instantly share code, notes, and snippets.

@venelrene
Last active October 4, 2023 16:09
Show Gist options
  • Save venelrene/e8480c2c968caa653dd067bd49b2df60 to your computer and use it in GitHub Desktop.
Save venelrene/e8480c2c968caa653dd067bd49b2df60 to your computer and use it in GitHub Desktop.
Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').
function isOdd(num) { return num & 1; };
function solution(str){
let arr = str.match(/.{1,2}/g);
if (str.length == 0) {
return []
} else if(isOdd(str.length) == 1) {
let x = str + "_"
return arr = x.match(/.{1,2}/g);
} else {
return arr;
}
}
// Refactored
function solution(str) {
if (str.length == 0) {
return []
};
return (str.length % 2 ? str + '_' : str).match(/../g);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment