Algorithm 8 - [JS] Simple Pig Latin

luneahΒ·2021λ…„ 12μ›” 4일
0

Algorithm CodeKata

λͺ©λ‘ 보기
8/13
post-thumbnail

Simple Pig Latin

Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

Examples :

pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !');     // elloHay orldway !

πŸ“Œ Needs ) λ‹¨μ–΄μ˜ 첫번째 κΈ€μžλ₯Ό 맨 끝으둜 κ°€κ²Œ ν•˜κ³  단어 끝에 "ay"λ₯Ό μΆ”κ°€ν•˜λΌ.

πŸ“ Sol ) split μ‚¬μš©ν•΄ 단어 μͺΌκ°œκ³  μƒˆλ‘œμš΄ 배열을 λ§Œλ“€μ–΄ join ν›„ return

function pigIt(str) {
  let result = [];
  let arr = str.split(' ');
    for ( let i = 0; i < arr.length; i++ ) { 
      result.push(arr[i].slice(1, arr[i].length) + arr[i][0] + 'ay')
    }
  return result.join(' ');
}

πŸ’‘ Other ways to solve ) map ν™œμš©ν•˜μ—¬ λ”μš± κ°„λ‹¨ν•˜κ²Œ μ½”λ“œ μ§€ 수 있음.

function pigIt(str) {
  return str.split(' ').map(function(el) {
    return el.slice(1) + el.slice(0,1) + 'ay';
  }).join(' ');
}
profile
ν•˜λŠ˜μ΄μ˜ 개발 일기

0개의 λŒ“κΈ€

Powered by GraphCDN, the GraphQL CDN