Last active
October 4, 2023 16:09
-
-
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 ('_').
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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