Повторите строку повторить строку
// Repeat a String Repeat a String
// Repeat a given string str (first argument) for num times (second argument).
// Return an empty string if num is not a positive number. For the purpose of this challenge, do not use the built-in .repeat() method.
function repeatStringNumTimes(str, num) {
if (num < 0) return '';
let result = '';
for (let i = 0; i <= num - 1; i++) {
result += str;
}
return result;
}
repeatStringNumTimes('abc', 3);
// OR
function repeatStringNumTimes(str, num) {
var accumulatedStr = '';
while (num > 0) {
accumulatedStr += str;
num--;
}
return accumulatedStr;
}
// OR
function repeatStringNumTimes(str, num) {
if (num < 1) {
return '';
} else {
return str + repeatStringNumTimes(str, num - 1);
}
}
// OR
// my favourite
function repeatStringNumTimes(str, num) {
return num > 0 ? str + repeatStringNumTimes(str, num - 1) : '';
}
YosKa