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 !
๋ฌธ์ ์ดํด
๋จ์ด์ ์ฒซ๋ฒ์งธ ์ํ๋ฒณ์ ๋งจ ๋ค๋ก ์ฎ๊ธด๋ค.
๋จ์ด๋ง๋ค ๋์ 'ay'๋ฅผ ๋ถ์ธ๋ค.
ํน์๋ฌธ์๋ ๊ทธ๋๋ก ๋์จ๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ
๋จ์ด๋ง๋ค ์ ๊ทผํ๊ธฐ ์ํด ๋ฌธ์์ด๋ฅผ ๋ฐฐ์ด๋ก ๋ง๋ ๋ค.
์ฌ์ฉํ๊ธฐ ์ข์ ๋ฉ์๋
concat() - ๋ฉ์๋๋ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ ๋ชจ๋ ๋ฌธ์์ด์ ํธ์ถ ๋ฌธ์์ด์ ๋ถ์ธ ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํ
'ay'๋ฅผ ๋ถ์ผ ๋ ์ฌ์ฉํ๊ธฐ ์ข์ ๊ฑฐ ๊ฐ๋ค.
slice() - ๋ฉ์๋๋ ๋ฌธ์์ด์ ์ผ๋ถ๋ฅผ ์ถ์ถํ๋ฉด์ ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํํฉ๋๋ค.
์ฒซ๋ฒ์งธ ์ํ๋ฒณ์ ์๋ฅผ ๋ ์ฌ์ฉํ๋ค.
split() - ๋ฉ์๋๋ String ๊ฐ์ฒด๋ฅผ ์ง์ ํ ๊ตฌ๋ถ์๋ฅผ ์ด์ฉํ์ฌ ์ฌ๋ฌ ๊ฐ์ ๋ฌธ์์ด๋ก ๋๋๋๋ค.
๋ฐฐ์ด๋ก ๋ง๋ค ๋ ์ ์ฉํ๋ค.
join() - ๋ฉ์๋๋ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ฐ๊ฒฐํด ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ญ๋๋ค.**
๋ฌธ์์ด๋ก return ํ ๋ ์ฌ์ฉํ๋ค.
!์ ?๋ ๊ทธ๋๋ก ๋์จ๋ค.
์ฝ๋ ๊ตฌํ
function pigIt(str) {
const result = str.split(' ').map(word => {
if (word !== '!' && word !== '?') {
word = word + word[0] + 'ay';
word = word.slice(1);
}
return word;
}).join(' ');
return result;
}
๊ฒฐ๊ณผ ๋ถ์
๋๋ค ํ ์คํธ ํต๊ณผ
@e.mihaylin's Solution
pigIt = s => s.split(' ').map(e => e.substr(1) + e[0] + 'ay').join(' ');
๋๋ ์ฒซ๋ฒ์งธ๋ฅผ ์ ๊ฑฐํ์ง๋ง.. ์ด ์์ค๋ ์ธ๋ฑ์ค 1๋ถํฐ ๋ฌธ์์ด์ ๋ง๋ค๊ธฐ ๋๋ฌธ์ slice๋ฅผ ํ ํ์๊ฐ ์๋ค.
๋์ ๋ด๊ฐ ํผ ๋ฌธ์ ๋ ๋ณ๊ฒฝ๋ ๋ฌธ์ ๋ผ ์ด ์๋ฃจ์ ์ '!'์ '?'์๋ 'ay'๊ฐ ๋ถ๋๋ค.
Last updated
Was this helpful?