7

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!

2
  • Use the String.prototype.repeat: uhrzeit.repeat(5) Commented Oct 19, 2018 at 9: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. Commented Oct 19, 2018 at 9:13

1 Answer 1

24

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));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.