Skip to content Skip to sidebar Skip to footer

I Want To Find Vowel Occurences In Javascript Using Switch Statement

I want to write a function with a switch statement to count the number of occurrences of any two vowels in succession in a line of text. For example, in the sentence For example: O

Solution 1:

You can use a regex to find amount of occurrences.

Original string: Pleases read this application and give me gratuity

Occurences: ea, ea, io, ui

Result: 4

Regex:

  • [aeiou] means any of these characters.
  • {2} exactly 2 of them (change to {2,} if you wanna match 2 or more characters in a row)
  • g don't stop after the first match (change to gi if you want to make it case insensitive)

function findOccurrences() {
  var str = "Pleases read this application and give me gratuity";
  var res = str.match(/[aeiou]{2}/g);
  return res ? res.length : 0;
}

var found = findOccurrences();

console.log(found);

EDIT: with switch statement

function findOccurrences() {
  var str = "Pleases read this application and give me gratuity";
  var chars = str.toLowerCase().split("");
  var count = 0;
  
  // Loop over every character
  for(let i = 0; i < chars.length - 1; i++) {
    var char = chars[i];
    var next = chars[i + 1];
    
    // Increase count if both characters are any of the following: aeiou
    if(isCorrectCharacter(char) && isCorrectCharacter(next)) {
      count++
    }
  }
  
  return count;
}

// Check if a character is any of the following: aeiou
function isCorrectCharacter(char) {
  switch (char) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
      return true;
    default:
      return false;
  }
}

var found = findOccurrences();

console.log(found);

Solution 2:

If you insist on using switch you'd also need a loop and a flag to mark if you have seen a vowel already.

function findOccurrences() {
  var str = "Pleases read this application and give me gratuity";
  var count = 0;
  let haveSeenVowel = false;

  for (const letter of str.toLowerCase()) {
    switch (letter) {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
        {
          if (haveSeenVowel) {
            count++;
            haveSeenVowel = false;
          } else {
            haveSeenVowel = true;
          }
          break;
        }
      default:
        haveSeenVowel = false
    }
  }

  return count
}

console.log(findOccurrences());

Solution 3:

    function findOccurances(str){
    var words = str.split(" ");
    var count=0;
    for(var i=0;i<words.length;i++){
        for(var j=0; j<words[i].length; j++){
            var char = words[i].slice(j,j+1).toLowerCase();
            var nextChar = words[i].slice(j+1,j+2).toLowerCase();
            switch(char){
                case "a":
                case "e":
                case "i":
                case "o":
                case "u":
                    switch(nextChar){
                        case "a":
                        case "e":
                        case "i":
                        case "o":
                        case "u":
                            count++;
                    }
            }
        }
        
    }
    return count;
}

var str = "Pleases read this application and give me gratuity";
var count = findOccurances(str);
alert(count);

Post a Comment for "I Want To Find Vowel Occurences In Javascript Using Switch Statement"