forked from HackYourFuture/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-step3.js
More file actions
44 lines (37 loc) · 845 Bytes
/
3-step3.js
File metadata and controls
44 lines (37 loc) · 845 Bytes
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
'use strict';
// use a 'for' loop
function repeatStringNumTimes(str, num) {
// repeat after me
let repeatString = "";
for (; num > 0;) {
repeatString += str;
num--;
}
return repeatString;
}
console.log('for', repeatStringNumTimesWithFor('abc', 3));
// use a 'while' loop
function repeatStringNumTimes(str, num) {
// repeat after me
let repeatString = "";
while (num > 0){
repeatString += str;
num--;
}
return repeatString;
}
console.log('while', repeatStringNumTimesWithWhile('abc', 3));
// use a 'do...while' loop
function repeatStringNumTimes(str, num) {
// repeat after me
let repeatString = "";
do {
repeatString += str;
num--;
} while (num > 0);
if (num < 0){
return "";
}
return repeatString;
}
console.log('while', repeatStringNumTimesWithDoWhile('abc', 3));