Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a2037a68a0bc2aef0006002 Challenge 340: Pig Latin Converter 28 challenge-340

--description--

Given a string, convert it to Pig Latin using the following rules:

  • If a word begins with a vowel ("a", "e", "i", "o", or "u"), add "way" to the end. For example, "universe" converts to "universeway".
  • If a word begins with one or more consonants, move them to the end and add "ay". For example, "hello" converts to "ellohay".
  • Preserve the case of the first letter. For example, "Hello" converts to "Ellohay".

--hints--

pigLatin("universe") should return "universeway".

assert.equal(pigLatin("universe"), "universeway");

pigLatin("hello") should return "ellohay".

assert.equal(pigLatin("hello"), "ellohay");

pigLatin("hello universe") should return "ellohay universeway".

assert.equal(pigLatin("hello universe"), "ellohay universeway");

pigLatin("Hello universe") should return "Ellohay universeway".

assert.equal(pigLatin("Hello universe"), "Ellohay universeway");

pigLatin("Pig Latin is fun") should return "Igpay Atinlay isway unfay".

assert.equal(pigLatin("Pig Latin is fun"), "Igpay Atinlay isway unfay");

pigLatin("The quick brown fox jumped over the lazy dog") should return "Ethay uickqay ownbray oxfay umpedjay overway ethay azylay ogday".

assert.equal(pigLatin("The quick brown fox jumped over the lazy dog"), "Ethay uickqay ownbray oxfay umpedjay overway ethay azylay ogday");

--seed--

--seed-contents--

function pigLatin(str) {

  return str;
}

--solutions--

function pigLatin(str) {
  const vowels = "aeiou";

  return str.split(" ").map(word => {
    const lowerWord = word.toLowerCase();

    if (vowels.includes(lowerWord[0])) {
      return word + "way";
    }

    let i = 0;
    while (i < word.length && !vowels.includes(lowerWord[i])) {
      i++;
    }

    const consonants = word.slice(0, i);
    const rest = word.slice(i);
    const result = rest + consonants + "ay";

    if (word[0] === word[0].toUpperCase()) {
      return result[0].toUpperCase() + result.slice(1).toLowerCase();
    }

    return result;
  }).join(" ");
}