Javascript - 重复的字符

时间:2012-04-11 12:47:39

标签: javascript

我正在尝试创建一个程序来重复输入的单词,因为它有多个字符,我尝试了几种不同的方法,这是迄今为止的工作。

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

我希望它重复打招呼的一个例子是......

Enter a word: Hello

Hello

Hello

Hello

Hello

Hello

4 个答案:

答案 0 :(得分:0)

只需添加以下内容:

for(var i = 0, repeat = word.length; i < repeat; i++) {
    console.log(word);
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试以下代码重复带有字符数的字词。

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

答案 3 :(得分:0)

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

尝试:http://jsfiddle.net/6skD7/2/