3

i want to display a string as many times I have a generated variable. Therefore I'd like to do something like that, which doesn't work

var shower_total = 7; // this gets generated, but for simplification... 
var uhrzeit = "<p class='clock_item'>Foo</p>";
document.getElementById("uhrzeit_block").innerHTML =5*uhrzeit;

That's why I tried looping it but that doesn't work neither

document.getElementById("uhrzeit_block").innerHTML = 
 for(b=0, b <= shower_total; b++){
   uhrzeit
};

What do I do wrong or what would be a possible - beginner-compatible - solution. Thanks!

Daiaiai
  • 1,019
  • 3
  • 12
  • 28
  • Use the [`String.prototype.repeat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat): `uhrzeit.repeat(5)` – Ram Oct 19 '18 at 09:07
  • _“That's why I tried looping it but that doesn't work neither”_ - this doesn’t make any sense in the first place - you can not “assign” a loop to anything. `foo.innerHTML += uhrzeit` inside a loop, now that would start making some sense. – misorude Oct 19 '18 at 09:13

1 Answers1

17

You could use String#repeat instead of a multiplication of a string. This does not work for value who can not converted to a number.

var uhrzeit = "<p class='clock_item'>Foo</p>";

console.log(uhrzeit.repeat(5));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392