Created
September 19, 2020 06:42
-
-
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 ('_').
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 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