简体   繁体   English

Javascript-重复字符

[英]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/ 试试: http : //jsfiddle.net/6skD7/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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