알고리즘 86 - Split Strings

박진현·2021년 7월 24일

Q.

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 ('_').

Examples:

solution('abc') // should return ['ab', 'c_']
solution('abcdef') // should return ['ab', 'cd', 'ef']

A)

function solution(str){
  let res = [];
   for (let i = 0; i <str.length; i += 2) {
     if (!str[i+1]) res.push(str[i]+'_')
     else res.push(str.slice(i,i+2))
   }
  return res
}
profile
Product Manager (ex-Frontend Developer)

0개의 댓글