Skip to content
Prev Previous commit
Next Next commit
homeowrk3
  • Loading branch information
Nouransaeed committed Aug 14, 2019
commit 58d7c4ae125ce33ddfcad57f2b03f6b187018172
60 changes: 40 additions & 20 deletions Week3/homework/step2-3.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
'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;
}
return result;

console.log('for', repeatStringNumTimesWithFor('abc', 3)),

// Replace this comment and the next line with your code
console.log(str, num, result);

return result;
}

console.log('for', repeatStringNumTimesWithFor('abc', 3));
//while loop

// Use a 'while' loop
function repeatStringNumTimesWithWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

// Replace this comment and the next line with your code
console.log(str, num, result);
let result = '';

let counter = 0;

while (counter < num) {

result += str;

counter++;

}

return result;
}

}
console.log('while', repeatStringNumTimesWithWhile('abc', 3));
// 'do...while' loop

// Use a 'do...while' loop
function repeatStringNumTimesWithDoWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

// Replace this comment and the next line with your code
console.log(str, num, result);
let i = 0;

if (num > 0) {

do {

result += str,

i++;

} while (i < num);

}

return result;
}
console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 0)),

console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3));

// Do not change or remove anything below this line
module.exports = {

repeatStringNumTimesWithFor,

repeatStringNumTimesWithWhile,

repeatStringNumTimesWithDoWhile,

};