repl.it
@QuemuelVital/

Pig Latin

Nodejs

Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay".

fork
loading
Files
  • index.js
index.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
function translatePigLatin(str) {

  let voels = ['a', 'e', 'i', 'o', 'u'];
  let regex = /[bcdfghjklmnpqrstvwxyz]+/;
  let boo = false;
  let newStr;

  for (let i = 0; i <= voels.length; i++) {
    if (str[0] === voels[i]) {
      boo = true;
      newStr = str.concat('way');
    }
  }

  if (boo === false) {
    newStr = str.replace(regex, '')
      .concat(str.match(regex) + 'ay');
  }

  return newStr;

}

translatePigLatin("algorithm");