zoukankan      html  css  js  c++  java
  • 字符统计函数封装

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>

    <body>
        <script>
            // [2] 封装函数,该函数接收两个参数,一个参数是目标字符串,一个参数为指定的单字符,作用是统计指定字符在目标字符串中出现的次数。
            // 示例:传入 "Xiao Xia and Xiao Ming" 和"X",函数调用的结果应该为3(即在目标字符串中X这个字符总共出现了3次)。

            /* 分析:声明函数,该函数拥有两个参数,要把结果返回 */
            /* 参数:第一个参数是目标字符串,第二个参数是要统计的字符 */
            /* 返回值:统计的字符出现次数 */
            function charCount(str, s) {
                var count = 0;
                /* 思路:遍历字符串,依次获取字符串中每一个字符,拿当前的字符和目标字符进行比较,如果匹配那么就+1 */
                for (var i = 0, len = str.length; i < len; i++) {
                    if (str.charAt(i) == s) {
                        count++;
                    }
                }
                return count;
            }

            var res1 = charCount("Xiao Xia and Xiao Ming", "X"); //3
            var res2 = charCount("Nice to meet u", "X"); //0
            var res3 = charCount("Nice to meet u", "N"); //1
            var res4 = charCount("Nice to meet u", "e"); //3
            console.log(res1, res2, res3, res4);
        </script>
    </body>

    </html>
  • 相关阅读:
    没有可持续集成的日子里
    Apache Avro 与 Thrift 比较
    CruiseControl.NET以及使用
    不靠谱产品经理的特征
    Hadoop 新 MapReduce 框架 Yarn 详解
    Storm On YARN
    iOS开发资源:推送通知相关开源项目--PushSharp、APNS-PHP以及Pyapns等
    Swift REPL入门介绍
    学习Swift,一定不能错过的10大开源项目!
    iOS8 Size Classes的理解与使用
  • 原文地址:https://www.cnblogs.com/huayang1995/p/12077214.html
Copyright © 2011-2022 走看看