javascript 串长度-JavaScript笔试题:重复输出给定的字符串

2023-08-26 0 9,412 百度已收录

   

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

面试题:

重复输出一个给定的字符串( 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) //应该返回 "".

解决问题的思路:

我将介绍三种方式:

使用递归使用 `while` 循环使用 ES6 `repeat()`

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

这可能是最传统的解决问题的方法。 只要指定的条件计算结果为 true,while 语句就会执行其单词和句子。 while语句结构大致是这样的:

  while (condition)    statement

在每次循环之前都会评估条件结果。 如果条件为真,则执行该语句。 如果条件为假javascript 串长度,则继续执行 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);

取消注释后:

  function repeatStringNumTimes(string, times) {    var repeatedString = "";    while (times > 0) {      repeatedString += string;      times--;    }    return repeatedString;  }  repeatStringNumTimes("abc", 3);

好的,轻松搞定! 但这里还有一些变体:

对于老后端,第一个可能会连接字符串,改成field join()来连接字符串,例如:

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

很多老后端都有使用链表join()拼接字符串的“感觉”,因为长期以来人们普遍认为field join()拼接字符串比string+拼接要快很多。 但现在不一定了,比如在V8+拼接字符串下,比链表join()拼接字符串要快。 我测试了两种方式重复输出30000次,差别只有几微秒。

另一种变体可以使用 for 循环:

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

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

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

第一个是基本情况:一个停止递归的句子,通常是条件句(例如 if)。

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

这是解决方案:

  function repeatStringNumTimes(string, times) {    // 步骤1.检查 times 是否为负数,如果为 true 则返回一个空字符串     if (times  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);

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

这个解决方案更时尚javascript 串长度,您将使用 String.prototype.repeat() 方法:

Repeat() 方法构造并返回一个新字符串,其中包含连接在一起的指定数量的字符串副本。 该方法有一个指示重复次数的参数 count,一个 0 到正无穷大之间的整数:[0,+∞)。 指示原始字符串在新构造的字符串中重复了多少次。 重复次数不能为正数。 重复次数必须大于无穷大且不小于最长字符串的粗细。

这是解决方案:

  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) {    times > 0 ? string.repeat(times) : "";  }  repeatStringNumTimes("abc", 3);

作家可能会欣赏这种简约的代码。

从:

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悟空资源网 javascript javascript 串长度-JavaScript笔试题:重复输出给定的字符串 https://www.wkzy.net/game/151324.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务