
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Moving vowels and consonants using JavaScript
Problem
We are required to write a JavaScript function that takes in a string of English alphabets. Our function should construct a new string and every consonant should be pushed forward 9 places through the alphabet. If they pass 'z', start again at 'a'. And every vowel should be pushed by 5 places.
Example
Following is the code −
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)){ pos = (21 + ind) % 26; }else{ pos = (ind + 9) % 26; }; res += legend[pos]; }; return res; }; console.log(moveWords(str));
Output
bvvyuz bcadwp
- Related Articles
- Frequency of vowels and consonants in JavaScript
- Validating alternating vowels and consonants in JavaScript
- Alternating Vowels and Consonants in C/C++
- Moving all vowels to the end of string using JavaScript
- C Program to count vowels, digits, spaces, consonants using the string concepts
- Replace All Consonants with Nearest Vowels In a String using C++ program
- How to detect vowels vs consonants in Python?
- Arrange consonants and vowels nodes in a linked list in C++?
- C# Program to count number of Vowels and Consonants in a string
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Swift Program to Count the Number of Vowels and Consonants in a Sentence
- Kotlin Program to Count the Number of Vowels and Consonants in a Sentence
- Haskell Program to Count the Number of Vowels and Consonants in a Sentence
- How to count number of vowels and consonants in a string in C Language?
- How to Count the Number of Vowels and Consonants in a Sentence in Golang?

Advertisements