Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想使用Switch语句在Javascript中查找元音出现的情况_Javascript_Html_Switch Statement - Fatal编程技术网

我想使用Switch语句在Javascript中查找元音出现的情况

我想使用Switch语句在Javascript中查找元音出现的情况,javascript,html,switch-statement,Javascript,Html,Switch Statement,我想用switch语句编写一个函数,计算一行文本中连续出现的任意两个元音的次数。例如,在句子中 例如: Original string=“Pleases read此应用程序并为我提供gratuity” 此类事件出现在字符串ea、ea、ui中。 产出:3 function findOccurrences() { var str = "Pleases read this application and give me gratuity"; var count =

我想用switch语句编写一个函数,计算一行文本中连续出现的任意两个元音的次数。例如,在句子中

例如:

Original string=“Pleases read此应用程序并为我提供gratuity”

此类事件出现在字符串ea、ea、ui中。

产出: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();
您可以使用来查找发生的次数

原始字符串:Pleases read此应用程序ion并给我gratuity

发生:eaeaioui

结果:4

正则表达式:

  • [aeiou]
    表示这些字符中的任何一个
  • {2}
    正好是其中的两个(如果要匹配一行中的两个或更多字符,请更改为
    {2,}
  • g
    在第一次匹配后不要停止(如果要使其不区分大小写,请更改为
    gi
函数findOccurrences(){
var str=“请阅读此申请并给我小费”;
var res=str.match(/[aeiou]{2}/g);
返回分辨率?分辨率长度:0;
}
var found=findOccurrences();

console.log(找到)如果你坚持使用switch,你还需要一个循环和一个标志来标记你是否已经看到了元音

函数findOccurrences(){
var str=“请阅读此申请并给我小费”;
var计数=0;
让haveSeenVowel=false;
for(str.toLowerCase()的常量字母){
开关(字母){
案例“a”:
案例“e”:
案例“i”:
案例“o”:
案例“u”:
{
如果(haveSeenVowel){
计数++;
haveSeenVowel=假;
}否则{
haveSeenVowel=真;
}
打破
}
违约:
haveSeenVowel=false
}
}
返回计数
}
log(findOccurrences())
函数findoccurance(str){
var words=str.split(“”);
var计数=0;

对于(var i=0;iIt,如果input str=“aeo”需要1或2的输出,则“application”中似乎缺少了“io”?我必须使用switch语句only@WaqasUmer我添加了一个额外的示例,它根据switch语句检查字符。您能解释一下您的代码以及它是如何解决问题的吗?它的工作方式与(条件){if(条件){}首先,*它接受字符串并将其打断,然后将句子中的每个单词保存到一个数组中。*执行for循环以遍历数组的每个元素,即:word.*另一个for循环用于遍历单词的每个字符。*char变量取字符变量中的第一个字符,nextChar变量取字符变量中字符旁边的字符ble.*外部for循环取句子的第一个单词,内部for循环遍历内部for循环中的整个单词。外部开关语句检查第一个字符是否为元音。如果是,则内部开关将检查下一个字符是否为元音-如果两个对应字符都是元音,则计数将为incremented@Skully它仅使用switch语句显示任意两个元音(给定句子中的ea、ea、io、ui)的出现次数。Count表示出现次数
    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);