Joseph Crandal f142eed6ef
Fix pig latin challenge description (#38018)
* Fix pig latin challenge description

Added statement for beginning with consonants and if a vowel does not begin with a vowel.

* fix: update description/instructions

* fix:  removed extra line break

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix:  removed extra line break

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2020-03-26 23:38:07 +01:00

2.4 KiB

id, title, isRequired, challengeType, forumTopicId
id title isRequired challengeType forumTopicId
aa7697ea2477d1316795783b Pig Latin true 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.

Tests

tests:
  - text: <code>translatePigLatin("california")</code> should return "aliforniacay".
    testString: assert.deepEqual(translatePigLatin("california"), "aliforniacay");
  - text: <code>translatePigLatin("paragraphs")</code> should return "aragraphspay".
    testString: assert.deepEqual(translatePigLatin("paragraphs"), "aragraphspay");
  - text: <code>translatePigLatin("glove")</code> should return "oveglay".
    testString: assert.deepEqual(translatePigLatin("glove"), "oveglay");
  - text: <code>translatePigLatin("algorithm")</code> should return "algorithmway".
    testString: assert.deepEqual(translatePigLatin("algorithm"), "algorithmway");
  - text: <code>translatePigLatin("eight")</code> should return "eightway".
    testString: assert.deepEqual(translatePigLatin("eight"), "eightway");
  - text: Should handle words where the first vowel comes in the middle of the word.  <code>translatePigLatin("schwartz")</code> should return "artzschway". 
    testString: assert.deepEqual(translatePigLatin("schwartz"), "artzschway");
  - text: Should handle words without vowels. <code>translatePigLatin("rhythm")</code> should return "rhythmay".
    testString: assert.deepEqual(translatePigLatin("rhythm"), "rhythmay");

Challenge Seed

function translatePigLatin(str) {
  return str;
}

translatePigLatin("consonant");

Solution

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;
}