Is there a vowel in there javascript?

The five letters a, e, i, o and u are called vowels. All other alphabets except these 5 vowels are called consonants.

Example 1: Count the Number of Vowels Using Regex

// program to count the number of vowels in a string

function countVowel[str] { 

    // find the count of vowels
    const count = str.match[/[aeiou]/gi].length;

    // return number of vowels
    return count;
}

// take input
const string = prompt['Enter a string: '];

const result = countVowel[string];

console.log[result];

Output

Enter a string: JavaScript program
5

In the above program, the user is prompted to enter a string and that string is passed to the countVowel[] function.

  • The regular expression [RegEx] pattern is used with the match[] method to find the number of vowels in a string.
  • The pattern /[aeiou]/gi checks for all the vowels [case-insensitive] in a string. Here,
    str.match[/[aeiou]/gi]; gives ["a", "a", "i", "o", "a"]
  • The length property gives the number of vowels present.

Example 2: Count the Number of Vowels Using for Loop

// program to count the number of vowels in a string

// defining vowels
const vowels = ["a", "e", "i", "o", "u"]

function countVowel[str] {
    // initialize count
    let count = 0;

    // loop through string to test if each character is a vowel
    for [let letter of str.toLowerCase[]] {
        if [vowels.includes[letter]] {
            count++;
        }
    }

    // return number of vowels
    return count
}

// take input
const string = prompt['Enter a string: '];

const result = countVowel[string];

console.log[result];

Output

Enter a string: JavaScript program
5

In the above example,

  • All the vowels are stored in a vowels array.
  • Initially, the value of the count variable is 0.
  • The for...of loop is used to iterate over all the characters of the string.
  • The toLowerCase[] method converts all the characters of a string to lowercase.
  • The includes[] method checks if the vowel array contains any of the characters of the string.
  • If any character matches, the value of count is increased by 1.

I'm supposed to write a function that takes a character [i.e. a string of length 1] and returns true if it is a vowel, false otherwise. I came up with two functions, but don't know which one is better performing and which way I should prefer. The one with RegEx is way simpler but I am unsure whether I should try to avoid using RegEx or not?

Without RegEx:

function isVowel[char] {
  if [char.length == 1] {
    var vowels = new Array["a", "e", "i", "o", "u"];
    var isVowel = false;

    for [e in vowels] {
      if [vowels[e] == char] {
        isVowel = true;
      }
    }

    return isVowel;
  }
}

With RegEx:

function isVowelRegEx[char] {
  if [char.length == 1] {
    return /[aeiou]/.test[char];
  }
}

Penny Liu

12.9k5 gold badges71 silver badges86 bronze badges

asked Mar 30, 2011 at 14:55

3

benchmark

I think you can safely say a for loop is faster.

I do admit that a regexp looks cleaner in terms of code. If it's a real bottleneck then use a for loop, otherwise stick with the regular expression for reasons of "elegance"

If you want to go for simplicity then just use

function isVowel[c] {
    return ['a', 'e', 'i', 'o', 'u'].indexOf[c.toLowerCase[]] !== -1
}

answered Mar 30, 2011 at 15:01

RaynosRaynos

164k56 gold badges347 silver badges394 bronze badges

7

Lots of answers available, speed is irrelevant for such small functions unless you are calling them a few hundred thousand times in a short period of time. For me, a regular expression is best, but keep it in a closure so you don't build it every time:

Simple version:

function vowelTest[s] {
  return [/^[aeiou]$/i].test[s];
}

More efficient version:

var vowelTest = [function[] {
  var re = /^[aeiou]$/i;
  return function[s] {
    return re.test[s];
  }
}][];

Returns true if s is a single vowel [upper or lower case] and false for everything else.

answered Mar 30, 2011 at 23:39

RobGRobG

137k30 gold badges167 silver badges205 bronze badges

4

cycles, arrays, regexp... for what? It can be much quicker :]

function isVowel[char]
{
    return char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u' || false;
}

answered Mar 30, 2011 at 15:14

EmmermanEmmerman

2,38115 silver badges9 bronze badges

7

function findVowels[str] {
  return str.match[/[aeiou]/ig];
}

findVowels['abracadabra']; // 'aaaaa'

Basically it returns all the vowels in a given string.

answered Jun 10, 2017 at 15:01

This is a rough RegExp function I would have come up with [it's untested]

function isVowel[char] {
    return /^[aeiou]$/.test[char.toLowerCase[]];
}

Which means, if [char.length == 1 && 'aeiou' is contained in char.toLowerCase[]] then return true.

answered Mar 30, 2011 at 15:05

Buhake SindiBuhake Sindi

86.2k27 gold badges165 silver badges223 bronze badges

Personally, I would define it this way:

function isVowel[ chr ]{ return 'aeiou'.indexOf[ chr[0].toLowerCase[] ] !== -1 }

You could also use ['a','e','i','o','u'] and skip the length test, but then you are creating an array each time you call the function. [There are ways of mimicking this via closures, but those are a bit obscure to read]

bpierre

10.3k2 gold badges24 silver badges27 bronze badges

answered Mar 30, 2011 at 15:01

cwallenpoolecwallenpoole

77.4k26 gold badges125 silver badges163 bronze badges

1

I kind of like this method which I think covers all the bases:

const matches = str.match[/[aeiou]/gi];
return matches ? matches.length : 0;

answered Apr 8, 2019 at 11:16

IntellidroidIntellidroid

1,0151 gold badge9 silver badges15 bronze badges

1

function isVowel[char]
{
  if [char.length == 1]
  {
    var vowels = "aeiou";
    var isVowel = vowels.indexOf[char] >= 0 ? true : false;

    return isVowel;
  }
}

Basically it checks for the index of the character in the string of vowels. If it is a consonant, and not in the string, indexOf will return -1.

answered Mar 30, 2011 at 15:00

HåvardHåvard

9,5621 gold badge39 silver badges46 bronze badges

2

I created a simplified version using Array.prototype.includes[]. My technique is similar to @Kunle Babatunde.

const isVowel = [char] => ["a", "e", "i", "o", "u"].includes[char];

console.log[isVowel["o"], isVowel["s"]];

answered May 17, 2020 at 13:34

Penny LiuPenny Liu

12.9k5 gold badges71 silver badges86 bronze badges

This is the way I did it, on the first occurrence of a vowel in any given word it breaks out of the loop and returns true.

const vowels = ["a", "e", "i", "o", "u", "y"];

function isVowel[word] {
    let result = false;
    for [let i = 0; i < word.length; i++] {
        if [vowels.includes[word[i]]] {
            result = true;
            break;
        }
    }
    return result;

answered Apr 23, 2021 at 14:01

YuniacYuniac

1061 silver badge10 bronze badges

1

Basically it returns all the vowels in a given string.

function vowels[s] {
  let vowels = ["a", "e", "i", "o", "u"];

  for[let v of s] {
    if[vowels.includes[v]]
        console.log[v];
  }
}

answered Aug 25, 2021 at 5:58

  //function to find vowel
const vowel = [str]=>{
  //these are vowels we want to check for
  const check = ['a','e','i','o','u'];
  //keep track of vowels
  var count = 0;
  for[let char of str.toLowerCase[]]
  {
    //check if each character in string is in vowel array
    if[check.includes[char]] count++;
  }
  return count;
}

console.log[vowel["hello there"]];

answered Jun 13, 2018 at 17:31

1

How do you check if a string has vowels in JS?

“how to check vowels in a string in javascript” Code Answer's.
const vowelCount = str => {.
let vowels = /[aeiou]/gi;.
let result = str. match[vowels];.
let count = result. length;.
console. log[count];.

How do you print a vowel in JavaScript?

Printing vowels in the string.
let s = "welcome".
let c, i..
for [i=0; i> n; ]..
You cant read in a string using 'cin'..
you need to print sum [no. of vowels] outside loop..

How do you find a vowel in a string?

To find the vowels in a given string, you need to compare every character in the given string with the vowel letters, which can be done through the charAt[] and length[] methods. charAt[] : The charAt[] function in Java is used to read characters at a particular index number.

Chủ Đề