just learning JS and am going through Algorithm challenges.
The below code should repeat a string (str) x (num) times.
So for example repeatStringNumTimes('*', 3) should be '***'. The below code does that... but there is an 'undefined' word that appears in the beginning of the output. Why is that?! I've defined all variables...
function repeatStringNumTimes(str, num) {
let len = str.length;
let string;
for (let i = 0; i < num; i++) {
for (let x = 0; x < len; x++) {
string += str[x];
}
}
return string;
}
console.log(repeatStringNumTimes('*', 10));
let string;tolet string = "";as string isundefinedinitiallylet string;as beinglet string = undefined;