1

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

<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

4 回答 4

0

只需添加以下内容:

for(var i = 0, repeat = word.length; i < repeat; i++) {
    console.log(word);
}
于 2012-04-11T12:51:05.303 回答
0
​var word = "Hello";
var letters = word.length;
for(var i = 0; i < letters; i++){
    document.write(word + '<br />');
}​​​​​
于 2012-04-11T12:51:21.887 回答
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>
于 2012-04-11T12:51:39.210 回答
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/

于 2012-04-11T12:52:31.370 回答