<万博manbetx平台> JavaScript面试题:重复输出一个给定的字符串-万博manbetx平台

JavaScript面试题:重复输出一个给定的字符串

10年服务1亿前端开发工程师

本文的思路主要来自《》,但是我对内容作了很多修改和补充。

其实这是可以作为一道很好的面试题,可以考察开发人员的综合能力。

面试题:

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

function repeatStringNumTimes(str, num) {
  return str;
}
repeatStringNumTimes("abc", 3);

提供测试情况:

repeatStringNumTimes("*", 3) //应该返回 "***".
repeatStringNumTimes("abc", 3) //应该返回 "abcabcabc".
repeatStringNumTimes("abc", 4) //应该返回 "abcabcabcabc".
repeatStringNumTimes("abc", 1) //应该返回 "abc".
repeatStringNumTimes("*", 8) //应该返回 "********".
repeatStringNumTimes("abc", -2) //应该返回 "".

解题思路:

我将介绍三种方法:

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

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

这可能是最常规的解题思路。while 语句只要指定的条件计算结果为true的时候,就执行其语句。while 语句结构大概是这样的:

while (condition)
  statement

在每次通过循环之前计算条件结果。如果条件为true,则执行语句。如果条件为false,则执行继续 while 循环之后的任何语句。

只要条件为true,语句就会执行。 这里是解决方案:

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);

去掉注释后:

function repeatStringNumTimes(string, times) {
  var repeatedString = "";
  while (times > 0) {
    repeatedString += string;
    times--;
  }
  return repeatedString;
}
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);

去掉注释后:

function repeatStringNumTimes(string, times) {
  if(times < 0) 
    return "";
  if(times === 1) 
    return string;
  else 
    return string + repeatStringNumTimes(string, times - 1);
}
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);

去掉注释后:

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

您可以使用三元表达式作为 if/else 语句的快捷方式,如下所示:

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

面试官可能会根据欣赏这样的简洁代码。

相关资源

赞(0) 打赏
未经允许不得转载:万博manbetx平台 » JavaScript面试题:重复输出一个给定的字符串

评论 8

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #-49

    这里提供新的两种
    var a = new Array(times);a.fill(string).join(”)
    ======================================
    var a = new Array(times+1);a.join(string)

    xxxxx2年前 (2017-02-27)回复
    • 赞你

      薛同学2年前 (2017-03-02)回复
  2. #-48

    666

    2年前 (2017-02-27)回复
  3. #-47

    用>> ,把复杂度降到log(N)

    xxx2年前 (2017-02-27)回复
  4. #-46

    方法2,当times=0的时候…所以第一句if判断是否应该 <=0 ?

    薛同学2年前 (2017-03-02)回复
  5. #-45

    function repeatStr(str, time){
    if(typeof time !== “number” || time <= 0 || !str){
    return "";
    } else {
    var oldStr = str;
    for(var i = 0; i < time – 1; i ++){
    str += oldStr
    }
    return str;
    }
    }

    0.0 好大的出入!!! 学习下先!

    上头2年前 (2017-03-20)回复

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏