|
2 | 2 |
|
3 | 3 | // Use a 'for' loop |
4 | 4 | function repeatStringNumTimesWithFor(str, num) { |
5 | | - // eslint-disable-next-line prefer-const |
6 | | - let result = ''; |
7 | | - |
8 | | - // Replace this comment and the next line with your code |
9 | | - console.log(str, num, result); |
10 | | - |
11 | | - return result; |
| 5 | + // eslint-disable-next-line prefer-const |
| 6 | + let result = " "; |
| 7 | + for (let i = 0; num > i; i++) { |
| 8 | + result += str; |
| 9 | + } |
| 10 | + return result; |
12 | 11 | } |
13 | 12 |
|
| 13 | + |
14 | 14 | console.log('for', repeatStringNumTimesWithFor('abc', 3)); |
15 | 15 |
|
16 | 16 | // Use a 'while' loop |
17 | 17 | function repeatStringNumTimesWithWhile(str, num) { |
18 | | - // eslint-disable-next-line prefer-const |
19 | | - let result = ''; |
20 | | - |
21 | | - // Replace this comment and the next line with your code |
22 | | - console.log(str, num, result); |
23 | | - |
24 | | - return result; |
| 18 | + // eslint-disable-next-line prefer-const |
| 19 | + let result = ''; |
| 20 | + while (num > 0) { |
| 21 | + result += str; |
| 22 | + num--; |
| 23 | + } |
| 24 | + return result; |
25 | 25 | } |
26 | 26 |
|
27 | 27 | console.log('while', repeatStringNumTimesWithWhile('abc', 3)); |
28 | 28 |
|
29 | 29 | // Use a 'do...while' loop |
30 | 30 | function repeatStringNumTimesWithDoWhile(str, num) { |
31 | | - // eslint-disable-next-line prefer-const |
32 | | - let result = ''; |
33 | | - |
34 | | - // Replace this comment and the next line with your code |
35 | | - console.log(str, num, result); |
36 | | - |
37 | | - return result; |
| 31 | + // eslint-disable-next-line prefer-const |
| 32 | + let result = ''; |
| 33 | + do { |
| 34 | + result += str; |
| 35 | + num--; |
| 36 | + } while (num > 0); |
| 37 | + |
| 38 | + return result; |
38 | 39 | } |
39 | 40 |
|
40 | 41 | console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3)); |
41 | 42 |
|
42 | 43 | // Do not change or remove anything below this line |
43 | 44 | module.exports = { |
44 | | - repeatStringNumTimesWithFor, |
45 | | - repeatStringNumTimesWithWhile, |
46 | | - repeatStringNumTimesWithDoWhile, |
| 45 | + repeatStringNumTimesWithFor, |
| 46 | + repeatStringNumTimesWithWhile, |
| 47 | + repeatStringNumTimesWithDoWhile, |
47 | 48 | }; |
0 commit comments