html - I want to find vowel occurences in Javascript using Switch statement - Stack Overflow

I want to write a function with a switch statement to count the number of occurrences of any two vowels

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:

Original string = “Pleases read this application and give me gratuity”.

Such occurrences in string ea, ea, ui.

Output: 3

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

    switch (str) {
        case 'a':
            count++;
        case 'A':
            count++
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            return 1;
        default:
            return 0;
    }
}

findOccurrences();

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:

Original string = “Pleases read this application and give me gratuity”.

Such occurrences in string ea, ea, ui.

Output: 3

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

    switch (str) {
        case 'a':
            count++;
        case 'A':
            count++
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            return 1;
        default:
            return 0;
    }
}

findOccurrences();
Share Improve this question edited Jul 14, 2020 at 13:03 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jul 13, 2020 at 11:02 Waqas UmerWaqas Umer 992 silver badges13 bronze badges 2
  • It seems you have missed 'io' in 'application' – Yury Tarabanko Commented Jul 13, 2020 at 11:10
  • if input str = "aeo" what output you want 1 or 2? – Lovesh Dongre Commented Jul 13, 2020 at 11:33
Add a ment  | 

3 Answers 3

Reset to default 4

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);

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());

    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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744265289a4565838.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信