[04.25.22] Coding test

Juyeon.it·2022년 4월 25일
0

Coding test

목록 보기
14/32

Mexican Wave

Description

Rules
1. The input string will always be lower case but maybe empty.
2. If the character in the string is whitespace then pass over it as if it was an empty seat
Example
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

My answer

function wave(str){
  let result = [];
  
  for (let i = 0; i < str.length; i++) {
    if (str[i] === ' '){
      continue;
    } else {
      result.push(str.slice(0, i) + str[i].toUpperCase() + str.slice(i+1))
    }
  }
  
  return result;
}

Other solutions

function wave(str){
    let result = [];
    
    str.split("").forEach((char, index) => {
        if (/[a-z]/.test(char)) {
            result.push(str.slice(0, index) + char.toUpperCase() + str.slice(index + 1));
        }
    });
    
    return result;
}
var wave=w=>[...w].map((a,i)=>w.slice(0,i)+a.toUpperCase()+w.slice(i+1)).filter(a=>a!=w)
const wave = str => 
  [...str].map((s, i) => 
      str.slice(0, i) + s.toUpperCase() + str.slice(i + 1) 
  ).filter(x => x != str);

0개의 댓글

관련 채용 정보