JavaScript interview question: Repeat output a given string

Source: Internet
Author: User

Interview questions

Repeats the output of a given string (the str first argument) n times (the num second argument) and num returns an empty string if the second argument is not a positive number.

function repeatstringnumtimes (str, num) {  return  str;} Repeatstringnumtimes ("ABC", 3);

Provide test conditions:

// should return "* * *". // should return "ABCABCABC". // should return "ABCABCABCABC". // should return "ABC". // should return "********". // should return "".
Thinking of solving problems

I'll cover three ways:

    1. Using ' while ' loop
    2. Use recursion
    3. Use ES6 ' repeat () '
Method 1: Repeat the output of a string through the ' while ' loop
function repeatstringnumtimes (string, times) {  var repeatedstring = "";    while (Times > 0) {    + = string;    Times--;  }   return repeatedstring;} Repeatstringnumtimes ("ABC", 3);

But here are a few variants:

For the old front end, the first one might be stitching strings, modifying the array join() concatenation string, for example:

function repeatstringnumtimes (string, times) {  var//while   (Times > 0) {    Repeatedarr.push (string);    Times--;  }   return repeatedarr.join ("");} Repeatstringnumtimes ("ABC", 3)

Many of the old front ends are useful for the join() "sentiment" of array concatenation strings, since it was widely assumed that array concatenation was join() + much faster than string concatenation. But not necessarily now, for example, V8 the + string, it is faster than the array join() stitching string. I used both methods to test 30,000 repetitions of the output, only a few milliseconds.

Another variant can be used for loops:

function repeatstringnumtimes (string, times) {  var repeatedstring = "";    for (var i = 0; I < times; i++) {    + = string  ;  } return repeatedstring;} Repeatstringnumtimes ("ABC", 3)
Method 2: Repeat the output of a string by conditional judgment and recursion

Recursion is a technique by which the function itself is repeatedly called until it reaches the result of an iterative operation. To make it work properly, you must include some key features of recursion.

function repeatstringnumtimes (string, times) {  if(Times < 0)     return "";   if(times = =1     )return  string  ; Else     return string + repeatstringnumtimes (string, times-1);} Repeatstringnumtimes ("ABC", 3);
Method 3: Use the ES6 ' repeat () ' method to repeat the output of a string

This solution is more trendy and you will use the String.prototype.repeat () method:

The repeat () method constructs and returns a new string that contains a copy of the specified number of strings that are concatenated together. This method has a parameter count that represents the number of repetitions, between 0 and positive infinity: [0, +∞]. Indicates how many times the original string was repeated in the newly constructed string. The number of repetitions cannot be negative. The number of repetitions must be less than infinity, and the length will not be greater than the longest string.

function repeatstringnumtimes (string, times) {  if (Times > 0)    return  String.repeat (times);   Else    return "";} Repeatstringnumtimes ("ABC", 3);

You can use the ternary expression as a shortcut to the If/else statement, as follows:

function repeatstringnumtimes (string, times) {  return times > 0? string.repeat (times): "";} Repeatstringnumtimes ("ABC", 3);

Reprint Address: http://www.css88.com/archives/7045

JavaScript interview question: Repeat output a given string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.