Oliver Eyton-Williams 0bd52f8bd1
Feat: add new Markdown parser (#39800)
and change all the challenges to new `md` format.
2020-11-27 10:02:05 -08:00

2.0 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
aa7697ea2477d1316795783b Pig Latin 5 16039

--description--

Pig Latin is a way of altering English Words. The rules are as follows:

- If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add "ay" to it.

- If a word begins with a vowel, just add "way" at the end.

--instructions--

Translate the provided string to Pig Latin. Input strings are guaranteed to be English words in all lowercase.

--hints--

translatePigLatin("california") should return "aliforniacay".

assert.deepEqual(translatePigLatin('california'), 'aliforniacay');

translatePigLatin("paragraphs") should return "aragraphspay".

assert.deepEqual(translatePigLatin('paragraphs'), 'aragraphspay');

translatePigLatin("glove") should return "oveglay".

assert.deepEqual(translatePigLatin('glove'), 'oveglay');

translatePigLatin("algorithm") should return "algorithmway".

assert.deepEqual(translatePigLatin('algorithm'), 'algorithmway');

translatePigLatin("eight") should return "eightway".

assert.deepEqual(translatePigLatin('eight'), 'eightway');

Should handle words where the first vowel comes in the middle of the word. translatePigLatin("schwartz") should return "artzschway".

assert.deepEqual(translatePigLatin('schwartz'), 'artzschway');

Should handle words without vowels. translatePigLatin("rhythm") should return "rhythmay".

assert.deepEqual(translatePigLatin('rhythm'), 'rhythmay');

--seed--

--seed-contents--

function translatePigLatin(str) {
  return str;
}

translatePigLatin("consonant");

--solutions--

function translatePigLatin(str) {
  if (isVowel(str.charAt(0))) return str + "way";
  var front = [];
  str = str.split('');
  while (str.length && !isVowel(str[0])) {
    front.push(str.shift());
  }
  return [].concat(str, front).join('') + 'ay';
}

function isVowel(c) {
  return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1;
}