forked from HackYourFuture/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathstep2-3.js
More file actions
61 lines (52 loc) · 1.33 KB
/
step2-3.js
File metadata and controls
61 lines (52 loc) · 1.33 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
59
60
61
'use strict';
// Use a 'for' loop
function repeatStringNumTimesWithFor(str, num) {
// eslint-disable-next-line prefer-const
let result = '';
if (num <= 0) {
result = '';
} else {
for (let i = 0; i < num; i++) {
result += str;
}
}
// Replace this comment and the next line with your code
console.log(str, num, result);
return result;
}
console.log('for', repeatStringNumTimesWithFor('abc', 3));
//***********************************
// Use a 'while' loop
function repeatStringNumTimesWithWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';
let counter = 0;
while (counter < num) {
result += str;
counter++;
}
return result;
}
console.log('while', repeatStringNumTimesWithWhile('abc', 3));
// Use a 'do...while' loop
function repeatStringNumTimesWithDoWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';
let i = 0;
if (num > 0) {
do {
result += str;
i++;
} while (i < num);
}
return result;
}
// Replace this comment and the next line with your code
//console.log(str, num, result);
console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3));
// Do not change or remove anything below this line
module.exports = {
repeatStringNumTimesWithFor,
repeatStringNumTimesWithWhile,
repeatStringNumTimesWithDoWhile,
};