新手需要一些帮助

时间:2013-06-17 22:17:27

标签: javascript

我的代码是:

<script language="Javascript" type="text/javascript">
var input;
var w1,w2,w3,w4,w5,w6,w7,w8,w9,w10;
var w1 = ' ';
var w2 = ' ';
var w3 = ' ';
var w4 = ' ';
var w5 = ' ';
var w6 = ' ';
var w7 = ' ';
var w8 = ' ';
var w9 = ' ';
var w10 = ' ';

do {
    alert("Enter 10 words");
    w1 = prompt("Enter First Word", " ");
    w2 = prompt("Enter Second Word", " ");
    w3 = prompt("Enter Third Word", " ");
    w4 = prompt("Enter Fourth Word", " ");
    w5 = prompt("Enter Fifth Word", " ");
    w6 = prompt("Enter Sixth Word", " ");
    w7 = prompt("Enter Seventh Word", " ");
    w8 = prompt("Enter Eighth Word", " ");
    w9 = prompt("Enter Nineth Word", " ");
    w10 = prompt("Enter Tenth Word", " ");
} while(w1,w2,w3,w4,w5,w6,w7,w8,w9,w10 == " ");

var myWords = new Array(10);
myWords[0] = w1;
myWords[1] = w2;
myWords[2] = w3;
myWords[3] = w4;
myWords[4] = w5;
myWords[5] = w6;
myWords[6] = w7;
myWords[7] = w8;
myWords[8] = w9;
myWords[9] = w10;

    for(i = 0; i < myWords.length; i++) {
        document.write(myWords[i] + "<br/>");
    } 
    document.write(myWords[0] + "<br/>");
    document.write(myWords[1] + " " + myWords[2] + "<br/>");
    document.write(myWords[3] + " " + myWords[4] + " " + myWords[5] + "<br/>");
    document.write(myWords[6] + " " + myWords[7] + " " + myWords[8] + " " +     myWords[9] + "<br/>");
</script>

如何将此更改为一个提示符()而不是十个?

3 个答案:

答案 0 :(得分:3)

将单词保留在数组中,然后使用for循环向用户提示每个单词。

var words = new Array(10);

alert("Enter " + words.length + " words");

for(var i = 0; i < words.length; i++) {
    words[i] = prompt("Enter Word " + (i + 1), " ");
}

答案 1 :(得分:2)

var myWords = [],
    word,
    thList = ['First', 'Second', 'Third', 'Fourth', 'Sixth', 'Seventh', 'Eighth', 'Nineth', 'Tenth'],
    i = 0,
    len = thList.length;

    for ( ; i < len; i++ ) {
        do {
            word = prompt("Enter " + thList[i] + " Word", " ");
        } while ( word == " " );
        myWords.push(word);
    }

// And then the document.write part

答案 2 :(得分:0)

这样的东西可以用于提示。

var words = prompt("Enter 10 words, separated by commas", "");
var array = words.split(" ");

你有你的数组,然后你的for循环或其他东西:P