Moving vowels and consonants using JavaScript

We are required to write a JavaScript function that takes in a string of English alphabets. Our function should construct a new string where every consonant is pushed forward 9 places through the alphabet, and every vowel is pushed backward 5 places. If they pass 'z', start again at 'a', and if they go before 'a', wrap around to 'z'.

Problem Breakdown

The transformation rules are:

  • Vowels (a, e, i, o, u): Move backward 5 positions
  • Consonants: Move forward 9 positions
  • Non-alphabetic characters: Keep unchanged
  • Wrapping: Use modulo arithmetic to handle alphabet boundaries

Example

Let's trace through "sample string":

const str = 'sample string';
const moveWords = (str = '') => {
    str = str.toLowerCase();
    const legend = 'abcdefghijklmnopqrstuvwxyz';
    const isVowel = char => 'aeiou'.includes(char);
    const isAlpha = char => legend.includes(char);
    let res = '';
    
    for(let i = 0; i < str.length; i++){
        const el = str[i];
        if(!isAlpha(el)){
            res += el;
            continue;
        };
        
        let pos;
        const ind = legend.indexOf(el);
        if(isVowel(el)){
            // Move vowels backward 5 positions (26 - 5 = 21)
            pos = (21 + ind) % 26;
        } else {
            // Move consonants forward 9 positions
            pos = (ind + 9) % 26;
        };
        res += legend[pos];
    };
    return res;
};

console.log(moveWords(str));
bvvyuz bcadwp

How It Works

The algorithm uses modulo arithmetic for alphabet wrapping:

  • For vowels: (21 + ind) % 26 moves backward 5 positions
  • For consonants: (ind + 9) % 26 moves forward 9 positions

Step-by-Step Example

// Let's trace a few characters from "sample"
const legend = 'abcdefghijklmnopqrstuvwxyz';

// 's' (consonant at index 18): (18 + 9) % 26 = 1 -> 'b'
console.log("'s' ->", legend[(18 + 9) % 26]);

// 'a' (vowel at index 0): (21 + 0) % 26 = 21 -> 'v'  
console.log("'a' ->", legend[(21 + 0) % 26]);

// 'm' (consonant at index 12): (12 + 9) % 26 = 21 -> 'v'
console.log("'m' ->", legend[(12 + 9) % 26]);
's' -> b
'a' -> v
'm' -> v

Complete Function with Testing

const moveWords = (str = '') => {
    str = str.toLowerCase();
    const legend = 'abcdefghijklmnopqrstuvwxyz';
    const isVowel = char => 'aeiou'.includes(char);
    const isAlpha = char => legend.includes(char);
    let res = '';
    
    for(let i = 0; i < str.length; i++){
        const el = str[i];
        if(!isAlpha(el)){
            res += el;
            continue;
        };
        
        let pos;
        const ind = legend.indexOf(el);
        if(isVowel(el)){
            pos = (21 + ind) % 26;  // Move backward 5
        } else {
            pos = (ind + 9) % 26;   // Move forward 9
        };
        res += legend[pos];
    };
    return res;
};

// Test with different inputs
console.log(moveWords('hello world'));
console.log(moveWords('JavaScript'));
console.log(moveWords('aeiou'));  // All vowels
console.log(moveWords('bcdfg'));  // All consonants
qzggj rjmgk
svinbxmdyk
vzdjp
klupo

Conclusion

This function efficiently transforms strings by shifting vowels backward 5 positions and consonants forward 9 positions using modulo arithmetic. The approach handles alphabet wrapping and preserves non-alphabetic characters.

Updated on: 2026-03-15T23:19:00+05:30

333 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements