This is the problem:
Complete the function splitPairs such that it splits the input string into pairs of characters. If the input string has a length that is odd, then it should replace the missing second character of the final pair with an underscore _. Note, an empty string should make your function produce an empty array.
Here is my code (it keeps timing out):
function splitPairs(input) {
let inputArray = input.split('');
let result = [];
if (inputArray.length % 2 !== 0) {
for (let i = 0; i < inputArray.length; i + 2) {
let pair = inputArray[i] + inputArray[i+1];
//push that onto the result array
result.push(pair);
}
result.push(inputArray[inputArray.length - 1] + '_');
} else {
for (let i = 0; i < inputArray.length; i + 2) {
let pair = inputArray[i] + inputArray[i+1];
result.push(pair);
}
}
return result;
}
What am I doing wrong and what is the proper way to solve this problem? It would be better if I could write the solution myself but I could use help to know what methods I should use to solve it