• 重复输出一个给定的字符串


      描述:重复输出一个给定的字符串(str第一个参数)n 次 (num第二个参数),如果第二个参数num不是正数的时候,返回空字符串。

      思路:我将介绍三种方法:

    1. 使用 while 循环
    2. 使用递归
    3. 使用ES6 `repeat()`

    方法1:通过 `while` 循环重复输出一个字符串

    function repeatStringNumTimes(string, times) {
      // 第1步. 常见一个空字符,用来寄存重复的字符串
      var repeatedString = "";
     
      // 第2步. 设置 while 循环的条件为(times > 0) 作为检查
      while (times > 0) { // 只要 times 大于 0, 语句就会执行
        // 执行语句 statement
        repeatedString += string; // 等价于 repeatedString = repeatedString + string; 
        times--; // 递减,等价于 times = times - 1; 
      }
      /* while循环逻辑
              条件        T/F    repeatedString += string   结果          次数
        1th   (3 > 0)    true    "" + "abc"                "abc"          2
        2th   (2 > 0)    true    "abc" + "abc"             "abcabc"       1
        3th   (1 > 0)    true    "abcabc" + "abc"          "abcabcabc"    0
        4th   (0 > 0)    false
        }
      */
      
      // 第3步. 返回重复字符串
      return repeatedString; // "abcabcabc"
    }
     
    repeatStringNumTimes("abc", 3);

      不过这里还可以有几个变种:对于老前端来说,首先一个可能会将字符串拼接,修改为 数组join()拼接字符串,例如:

        function repeatStringNumTimes(string, times) {
          var repeatedArr = []; 
          while (times > 0) {
            repeatedArr.push(string);
            times--;
          }
          return repeatedArr.join("");
        }
        repeatStringNumTimes("abc", 3)

      很多老前端都有用数组join()拼接字符串的“情怀”,因为很早以前普遍认为数组join()拼接字符串比字符串+拼接速度要快得多。不过现在未必,例如,V8 下+拼接字符串,要比数组join()拼接字符串快。我用这两个方法测试了3万次重复输出,只相差了几毫秒。

      另一个变种可以用 for 循环:

        function repeatStringNumTimes(string, times) {
          var repeatedString = "";
          for(var i = 0; i < times ;i++) {
            repeatedString += string;
          }
          return repeatedString;
        }
        repeatStringNumTimes("abc", 3)

    方法2:通过条件判断和递归重复输出一个字符串

      递归是一种通过重复地调用函数本身,直到它达到达结果为止的迭代操作的技术。为了使其正常工作,必须包括递归的一些关键特征。

      第一种是基本情况:一个语句,通常在一个条件语句(如if)中,停止递归。

      第二种是递归情况:调用递归函数本身的语句。

      这里是解决方案:

    function repeatStringNumTimes(string, times) {
      // 步骤1.检查 times 是否为负数,如果为 true 则返回一个空字符串 
      if (times <= 0) {
        return "";
      }
      
      // 步骤2.检查times是否等于1,如果是,返回字符串本身。
      if (times === 1) {
        return string;
      }
      
      // 步骤3. 使用递归
      else {
        return string + repeatStringNumTimes(string, times - 1); // return "abcabcabc";
      }
      /* 
        递归方法的第一部分你需要记住,你不会只调用一次,您将有好几个嵌套调用
                     times       string + repeatStringNumTimes(string, times - 1)
          1st call   3           "abc" + ("abc", 3 - 1)
          2nd call   2           "abc" + ("abc", 2 - 1)
          3rd call   1           "abc" => if (times === 1) return string;
          4th call   0           ""   => if (times <= 0) return "";
        递归方法的第二部分
          4th call will return      ""
          3rd call will return     "abc"
          2nd call will return     "abc"
          1st call will return     "abc"
        最后调用是串联所有字符串
        return "abc" + "abc" + "abc"; // return "abcabcabc";
      */
    }
    repeatStringNumTimes("abc", 3);

      注意:递归的三个步骤

    方法3:使用ES6 `repeat()` 方法重复输出一个字符串

      这个解决方案比较新潮,您将使用 String.prototype.repeat() 方法:

      repeat() 方法构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。 这个方法有一个参数 count 表示重复次数,介于0和正无穷大之间的整数 : [0, +∞) 。表示在新构造的字符串中重复了多少遍原字符串。重复次数不能为负数。重复次数必须小于 infinity,且长度不会大于最长的字符串。

      这里是解决方案:

    function repeatStringNumTimes(string, times) {
      //步骤1.如果 times 为正数,返回重复的字符串
      if (times > 0) { // (3 > 0) => true
        return string.repeat(times); // return "abc".repeat(3); => return "abcabcabc";
      }
      
      //Step 2. Else 如果times是负数,如果为true则返回一个空字符串
      else {
        return "";
      }
    }
     
    repeatStringNumTimes("abc", 3);

      您可以使用三元表达式作为 if/else 语句的快捷方式,如下所示,谁都会欣赏这样的简洁代码:

    function repeatStringNumTimes(string, times) {
       return times > 0 ? string.repeat(times) : "";
    }
    repeatStringNumTimes("abc", 3);

      此种方法主要是采用了ES6的repeat方法,简便了许多。

  • 相关阅读:
    三种方式重启NGINX 简单
    转MongoDB、HandlerSocket和MySQL性能测试及其结果分析 简单
    php ini_set post_max_size,upload_max_filesize 简单
    grep 命令 简单
    ANSI,GBK,UTF8,UTF16LE,UTF16BE 简单
    提升工作效率软件 简单
    会议记录 简单
    第一章 :zabbix监控
    第七章 :分布式监控与SNMP监控
    linux系统安装SNMP(可用)
  • 原文地址:https://www.cnblogs.com/goloving/p/7691380.html
Copyright © 2020-2023  润新知