简体   繁体   English

计算某些字符串值的更好方法?

[英]Better way to count certain string values?

I was hoping to get some advice on a script I'm playing with.我希望得到一些关于我正在玩的剧本的建议。 I'm working on a program that counts numerous elements of a sentence and right now I'm just counting the vowels in total.我正在开发一个计算句子中多个元素的程序,现在我只是计算元音的总数。 I have a working script but I was wondering, is there a better way to do this than what I have so far?我有一个工作脚本,但我想知道,有没有比我目前拥有的更好的方法来做到这一点?

HTML HTML

<div id="test">
    <input type="text" />
    <p></p>
    <p></p>
</div>

JS JS

$(document).ready(function(){
    var key;
    var vowels;
    $("#test input[type='text']").on("keydown", function(e){
        key = e.target.value;
        $("#test p:nth-of-type(1)").text(key);
    })
    $(this).on("keydown", function(e){
        vowels = [];
        if(e.keyCode == 13){
            console.log($("#test input[type='text']").val());
            for(var i = 0; i < $("#test input[type='text']").val().length; i++){
                switch($("#test input[type='text']").val()[i]){
                    case "a":
                        vowels.push($("#test input[type='text']").val()[i]);
                        break;
                    case "e":
                        vowels.push($("#test input[type='text']").val()[i]);
                        break;
                    case "i":
                        vowels.push($("#test input[type='text']").val()[i]);
                        break;
                    case "o":
                        vowels.push($("#test input[type='text']").val()[i]);
                        break;
                    case "u":
                        vowels.push($("#test input[type='text']").val()[i]);
                        break;
                }
            }
            $("#test p:nth-of-type(2)").text("There are " +vowels.length +" vowels.");
        }
    })
})

Here's the Working Pen .这是工作笔

You can optimize for simplicity and speed using a temporary variable:您可以使用临时变量优化简单性和速度:

 if(e.keyCode == 13){
            var tmp=$("#test input[type='text']").val();
            console.log(tmp);
            for(var i = 0; i < tmp.length; i++){
                switch(tmp[i]){
                    case "a":
                        vowels.push("a");
                        break;
                    case "e":
                        vowels.push("e");
                        break;
                    case "i":
                        vowels.push("i");
                        break;
                    case "o":
                        vowels.push("o");
                        break;
                    case "u":
                        vowels.push("u");
                        break;
                }
            }

because parsing html countless times was slow and inflated code lines made it harder to see other problems.因为无数次解析 html 很慢而且膨胀的代码行使得更难看到其他问题。

Instead of代替

 $(this).on("keydown", ...

you could check for "keyup" since it gives updated content with it.您可以检查“keyup”,因为它提供了更新的内容。

You can actually simplify your code a lot more and remove the switch statement completely.您实际上可以进一步简化代码并完全删除 switch 语句。

In this approach, I use .match(/[aeiou]/gi) to generate an array of vowels.在这种方法中,我使用.match(/[aeiou]/gi)来生成元音数组。 The g flag will match all occurrences, and the i flag will ignore the character's case. g标志将匹配所有出现,而i标志将忽略字符的大小写。

Updated Example更新示例

$('#test :input').on('input keydown', function(e) {
  var input = this.value,
      match = input.match(/[aeiou]/gi),
      count = match ? match.length : 0;

  $('#test p').eq(0).text(input);

  if (e.keyCode === 13) {
    $('#test p').eq(1).text('There are ' + count + ' vowels.');
  }
});
if(e.keyCode == 13){
    var tmp=$("#test input[type='text']").val();
    for(var i = 0; i < tmp).length; i++){
        if(["a", "e", "i", "o", "u"].indexOf(tmp[i]))
        vowels.push(tmp[i]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM