Sobes.tech
Middle+

Algorithm task: write a function that reversibly changes vowels in a word.

sobes.tech AI

Answer from AI

To mirror vowels in a word, you need to find all the vowels, keep their order, and then replace them in reverse order. For example, in the word "привет" (privet), the vowels "и" and "е" will swap places, resulting in "превит".

Example in JavaScript:

function mirrorVowels(word) {
  const vowels = 'аеёиоуыэюяAEЁИОУЫЭЮЯ';
  const chars = word.split('');
  const vowelIndices = [];
  const vowelChars = [];

  // Collect vowels and their indices
  chars.forEach((ch, i) => {
    if (vowels.includes(ch)) {
      vowelIndices.push(i);
      vowelChars.push(ch);
    }
  });

  // Replace vowels in reverse order
  vowelIndices.forEach((idx, i) => {
    chars[idx] = vowelChars[vowelChars.length - 1 - i];
  });

  return chars.join('');
}

console.log(mirrorVowels('привет')); // превит
Algorithm task: write a function that reversibly… - sobes.tech