Why below block of code not output string ?
I was expecting it should display abc when we pass num=1.
What I am missing here ?
function repeatStringNumTimes(str, num) {
return Array(num).join(str);
}
console.log(repeatStringNumTimes("abc", 1));
You need at least two elements for Array#join with a separator, because one element is just converted to a string and it does not need any glue.
function repeatStringNumTimes(str, num) {
return Array(num + 1).join(str);
}
console.log(repeatStringNumTimes("abc", 1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With