-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeat_a_string_repeat_a_string.js
More file actions
58 lines (54 loc) · 1.37 KB
/
Repeat_a_string_repeat_a_string.js
File metadata and controls
58 lines (54 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const repeatStringNumTimes = (str, num) => num > 0 && num != Infinity ? str += repeatStringNumTimes(str, num-1) : "";
/*
I was playing around with a less complex solution and I came up with this:
const repeatStringNumTimes = (str, num) => (num > 0) ? new Array(num).fill(str).join('') : "";
I actually like this solution. And it's fairly quick.
*/
/*
Note: I do know about the ES6 String.prototype.repeat()
I didn't want to use it. I wanted to stay with the nature of the challenge.
Also, using negative numbers results in an error with .repeat()
*/
/*
function repeatStringNumTimes(str, num) {
if (num <= 0) return "";
var result = "";
for (var i = 0; i <num; i++) {
result += str;
}
return result;
}
*/
/*
function repeatStringNumTimes(str, num) {
if (num <= 0) return "";
return str.repeat(num);
}
*/
/*
function repeatStringNumTimes(str, num) {
if (num <= 0) return "";
var result = new Array(num).fill(str);
return result.join('');
}
*/
/* Testing for my ES6 refactor of the old code below.
function repeatStringNumTimes(str, num) {
// repeat after me
if (num > 0) {
return str += repeatStringNumTimes(str, num-1);
}
return "";
}
*/
/* Old code
function repeatStringNumTimes(str, num) {
var result = "";
while (num > 0) {
result += str;
num -= 1;
}
return result;
}
*/
repeatStringNumTimes("abc", 3);