RepeatStringNumTimes - FreeCodeCamp
by Joel Garcia
HTML
<h1>Repeat String Num Times</h1>
<div id="result"></div>
CSS
h1 {
text-align: center;
font-size: 3em
}
JavaScript
/**
Concatenar valores con :-->>> +=
*/
function repeatStringNumTimes(str,num, stringPerNumTimes = "") {
while ( num > 0 ) {
//Concatena
stringPerNumTimes += str;
//Resta una vuelta
num--;
}
//Muestra
console.log(stringPerNumTimes);
document.getElementById("result").innerHTML += stringPerNumTimes + '<br />'
//Devuelve
return stringPerNumTimes;
}
repeatStringNumTimes("*",3);
repeatStringNumTimes("abc",3);
repeatStringNumTimes("abc",4);
repeatStringNumTimes("abc",1);
repeatStringNumTimes("*",8);
repeatStringNumTimes("abc",-2);
repeatStringNumTimes("abc",0);