简体   繁体   中英

Javascript - Repeated Characters

I'm trying to create a program to repeat the word entered for as many times as it has characters, I've tried several different methods which are not working, here is the work so far.

<html>
<head>
<title>Repeat Word</title>
<script type="text/javascript">
//Program: Repeat Word
//Purpose: Repeat word for as many times as it has characters
//Date last modified: 4/11/12 
var word = "";

word = prompt("Enter the word you would like repeated?")


</script>
</head>
<body>
</form>
</body>
</html>

An example of what I want it to repeat for hello would be...

Enter a word: Hello

Hello

Hello

Hello

Hello

Hello

Just add the following:

for(var i = 0, repeat = word.length; i < repeat; i++) {
    console.log(word);
}
​var word = "Hello";
var letters = word.length;
for(var i = 0; i < letters; i++){
    document.write(word + '<br />');
}​​​​​

Try below code to repeat words with number of characters.

<html>
<head>
<title>Repeat Word</title>
<script type="text/javascript">
//Program: Repeat Word
//Purpose: Repeat word for as many times as it has characters
//Date last modified: 4/11/12 
var word = "";

word = prompt("Enter the word you would like repeated?")
for(var i=0;i<word.length;i++)
{
 document.write(word+"<br />");
}

</script>
</head>
<body>
</form>
</body>
</html>
<body>
     <script type="text/javascript">
           var repeat = prompt("Enter the word you would like repeated");
           for(var i=0;i<=repeat.length;i=i+1) {
                document.write(repeat + "<br />");
           }
     </script>
</body>

Try: http://jsfiddle.net/6skD7/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM