Function keeps timing out

Viewed 206

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

4 Answers

You need to increment using i+=2. Also, there are some mistakes in the solution:

function splitPairs(input) {
  let inputArray = input.split('');
  let result = [];
  if(!inputArray)
    return result;
  if (inputArray.length % 2 !== 0) {
    for (let i = 0; i < inputArray.length-1; i+=2) {
      let pair = inputArray[i] + inputArray[i+1];
      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;
}

console.log(splitPairs(""));

console.log(splitPairs("abcd"));

console.log(splitPairs("abcde"));

A simpler solution (with one loop) as mentioned in the comments would be:

function splitPairs(input) {
  let inputArray = input.split('');
  let result = [];
  if(!inputArray)
    return result;
  let odd = (inputArray.length % 2 !== 0);
  let len = (odd) ? inputArray.length-1 : inputArray.length;
  for (let i = 0; i < len; i+=2) {
    let pair = inputArray[i] + inputArray[i+1];
    result.push(pair);
  }
  if(odd)
    result.push(inputArray[inputArray.length - 1] + '_');    
  return result;
}

console.log(splitPairs(""));

console.log(splitPairs("abcd"));

console.log(splitPairs("abcde"));

It's timing out because you're not incrementing i. i + 2 computes the new value but doesn't assign it anywhere. You can update i by doing i += 2 which is shorthand for i = i + 2.

You can use a one(two) liner

var result=str.split(/(..)/).filter(v=>v)
if (result[result.length-1].length==1) result[result.length-1]+="_"

You can do it like this

function splitPairs(input) {
  return input.split('').map((c, i) => {
    if (i % 2 !== 0) return;
    if (input[i+1]) {
      return input[i] + input[i+1];
    }    

    return input[i] + '_';
  }).filter(pair => pair);
}
Related