JavaScript exercises - statistics include the number string "a" or "A" of

Title FIG output
Title Description

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>统计</title>
</head>
<body>

//错误结果
<input name="统计结果" type="button" value="统计结果1" onclick="statistic()">

//正确结果
<input name="统计结果"type="button"value="统计结果2"onclick="statistic1()">
</body>
<script>
    var arr=new Array("America","Greece","Britain","Canada","China","Egypt");
    var  count=0;
    //但是这样有一个问题,就是当他一个单词检测到有a或者A之后就不再继续下去了,得到的结果只有4
    function statistic() {
        for (var i=0;i<arr.length;i++){
            if (arr[i].indexOf("a")!=-1 || arr[i].indexOf("A")!=-1){
                count++;
            }
        }
        document.write(count);
    }

    //把这一个字符串又放到一个数组里去查看是否有aA
    function statistic1() {
        var arrs=new Array();
        for (var i=0;i<arr.length;i++){
            //把arr[i]放入arrs这个数组里
            arrs=arr[i];
            //循环遍历arrs这个数组
            for (var j=0;j<arrs.length;j++){
                //indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果没有找到匹配的字符串则返回 -1
                if (arrs[j].indexOf("a")!=-1 || arrs[j].indexOf("A")!=-1){
                    count++;
                }
            }
        }
        document.write(count);
    }
</script>
</html>

Two results are not the same

Published 23 original articles · won praise 2 · Views 1039

Guess you like

Origin blog.csdn.net/weixin_46101839/article/details/104337353