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
56 lines (43 loc) · 1.15 KB
/
step2-3.js
File metadata and controls
56 lines (43 loc) · 1.15 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
'use strict';
// Use a 'for' loop
function repeatStringNumTimesWithFor(str, num) {
// eslint-disable-next-line prefer-const
let result = '';
for (let i = 0; i < num; i++) {
result = +str;
num--;
}
return result;
}
// but when I console.log it returns : for NaN!!!!
console.log('for', repeatStringNumTimesWithFor('abc', 3));
/*************************************************************************/
// Use a 'while' loop
function repeatStringNumTimesWithWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';
while (num > 0) {
result += str;
num--;
}
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;
do {
result = +str;
i++;
} while (i < num);
return result;
}
console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3));
// Do not change or remove anything below this line
module.exports = {
repeatStringNumTimesWithFor,
repeatStringNumTimesWithWhile,
repeatStringNumTimesWithDoWhile,
};