Skip to content

Instantly share code, notes, and snippets.

@hassan-maavan
Created September 19, 2020 06:42
Show Gist options
  • Save hassan-maavan/98d54cc8e5d99d3fa4783f88254c479f to your computer and use it in GitHub Desktop.
Save hassan-maavan/98d54cc8e5d99d3fa4783f88254c479f 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 solution(str){
var str_array = str.split('');
var result = [];
str_array.forEach((value, key) => {
if(key % 2 == 0) {
let next = (str_array[key + 1] ? str_array[key + 1] : '_')
result.push(value + next);
}
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment