javascriptloopsfor-loop

How to duplicate a string 10 times


I have a prompt that asks the user to enter one word and then I want to duplicate this word 10 times using a for loop and show the result in an alert box.

It's just I can't seem to figure out what I need to enter inside my for loop to get it to duplicate my message variable ten times and then save the result to another variable which I can show in my alert box. Does someone know how to do this in Javascript?

Edit: This is the code I tried now:

function button() {
    var message = prompt ("Enter a word", "");
    for(var i = 0; i < 5; i++) {
        message += message;
    }
    alert (message);
}

Solution

  • var message = prompt("Enter a message", ""),
        newMessage = '';
    
    for(var i=0; i<10; i++){
      newMessage += message;
    }
    
    alert(newMessage);