Простая реализация Pig Latin в JavaScript

Я могу помочь вам с простой реализацией pig Latin в JavaScript. Pig Latin — это языковая игра, в которой слова изменяются путем перемещения первой буквы в конец слова и добавления «ау». Например, «привет» становится «эллохай». Вот пример того, как это можно реализовать в JavaScript:

function pigLatin(word) {
  // Convert the word to lowercase for consistency
  word = word.toLowerCase();
  // Check if the word starts with a vowel
  const vowels = ['a', 'e', 'i', 'o', 'u'];
  if (vowels.includes(word[0])) {
    return word + 'ay';
  } else {
    return word.slice(1) + word[0] + 'ay';
  }
}
function pigLatinSentence(sentence) {
  // Split the sentence into an array of words
  const words = sentence.split(' ');
  // Apply pig Latin transformation to each word
  const pigLatinWords = words.map(word => pigLatin(word));
  // Join the words back to form a sentence
  const pigLatinSentence = pigLatinWords.join(' ');
  return pigLatinSentence;
}
// Example usage
const sentence = 'Hello world! This is a test.';
const pigLatinSentence = pigLatinSentence(sentence);
console.log(pigLatinSentence); // Output: 'ellohay orldway! histay isay aay esttay.'

Этот код определяет две функции: pigLatinи pigLatinSentence. Функция pigLatinпреобразует одно слово в свиную латынь, а функция pigLatinSentenceпреобразует целое предложение в свиную латынь, применяя преобразование к каждому слову.