programming-examples/js/String/Repeat a string a specified times.js

14 lines
471 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function repeat_string(string, count)
{
if ((string == null) || (count < 0) || (count === Infinity) || (count == null))
{
return('Error in string or count.');
}
count = count | 0; // Floor count.
return new Array(count + 1).join(string);
}
console.log(repeat_string('a', 4.6));
console.log(repeat_string('a'));
console.log(repeat_string('a', -2));
console.log(repeat_string('a', Infinity));