-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsimple-pig-latin.js
42 lines (36 loc) · 1.17 KB
/
simple-pig-latin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
// pigIt('Hello world !'); // elloHay orldWay !
function pigIt(str){
// split string on space
// map the words
// word with the first letter at the end and ay
// join on space
return str.split(' ').map(word => word.slice(1) + word[0] + 'ay').join(' ');
}
// pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
// pigIt('Hello world !'); // elloHay orldWay !
function pigIt(str){
// a place to store the pig latin
let pigLatin = ''
let firstLetter = str[0];
// iterate over the string
for (let i = 1; i < str.length; i++) {
const currentLetter = str[i];
if (!firstLetter) {
firstLetter = currentLetter;
} else {
if (currentLetter != ' ') {
// append each next letter until we see a space
pigLatin += currentLetter;
} else {
// append the current first letter and ay
pigLatin += firstLetter + 'ay ';
firstLetter = '';
}
}
}
pigLatin += firstLetter + 'ay';
return pigLatin;
}
console.log(pigIt('Pig latin is cool'),'igPay atinlay siay oolcay')
console.log(pigIt('This is my string'),'hisTay siay ymay tringsay')