
- 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
Validating alternating vowels and consonants in JavaScript
Problem
We are required to write a JavaScript function that takes in a string of English alphabets, str, as the first and the only argument. Our function should return true if and only if the vowels and consonants appear alternatingly in the input string, false otherwise.
For example, if the input to the function is −
Input
const str = 'amazon';
Output
const output = true;
Output Explanation
Because the vowels and consonants appear alternatingly in the string ‘amazon’.
Example
Following is the code −
const str = 'amazon'; const appearAlternatingly = (str = '') => { return str.split('').every((v,i)=>{ if (/[aeiou]/.test(str[0])){ if (i%2===0&&/[aeiou]/.test(v)){ return true } else if (i%2!==0&&!/[aeiou]/.test(v)){ return true } else { return false } } if (!/[aeiou]/.test(str[0])){ if (i%2==0&&!/[aeiou]/.test(v)){ return true } else if (i%2!==0&&/[aeiou]/.test(v)){ return true } else { return false } } }) }; console.log(appearAlternatingly(str));
Output
true
- Related Articles
- Alternating Vowels and Consonants in C/C++
- Moving vowels and consonants using JavaScript
- Frequency of vowels and consonants in JavaScript
- 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
- Validating email and password - JavaScript
- 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
- Haskell 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
- 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?
- Replace All Consonants with Nearest Vowels In a String using C++ program
- Validating a power JavaScript

Advertisements