There was an error while loading. Please reload this page.
1 parent 4dff23e commit d9b1f22Copy full SHA for d9b1f22
7_Repeat-String/index.js
@@ -0,0 +1,28 @@
1
+// Repeat String
2
+// Write a function that takes in a string and a number, and repeats the string that number of times
3
+
4
+const repeatString = (str, num) => {
5
+ let repeatedStr = ''
6
+ while (num > 0) {
7
+ repeatedStr += str
8
+ num--
9
+ }
10
+ return repeatedStr
11
+}
12
13
+console.log(repeatString('Hello', 4))
14
+console.log(repeatString('Bacon', 3))
15
+console.log(repeatString('hey', 1))
16
+console.log(repeatString('word', 0))
17
18
+//
19
20
+// or super simple
21
+const easyStrRepeat = (str, num) => {
22
+ return str.repeat(num)
23
24
25
+console.log(easyStrRepeat('Hello', 4))
26
+console.log(easyStrRepeat('Bacon', 3))
27
+console.log(easyStrRepeat('hey', 1))
28
+console.log(easyStrRepeat('word', 0))
0 commit comments