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:
- Using ' while ' loop
- Use recursion
- 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