我如何在for循环中执行此操作?

时间:2015-03-23 14:51:45

标签: javascript

我被告知我的幸运数字math.random代码是重复代码,并将其与输出字符串或用户for循环组合。我如何在for循环中执行此操作?

function showFortune() {
    // Quotes to grab from array.
    var quotes = new Array(5)
    quotes[0] = "The road to riches is paved with homework.";
    quotes[1] = "Ignore your previous fortune.";
    quotes[2] = "Avoid taking unnecessary gambles.";
    quotes[3] = "You love peace.";
    quotes[4] = "A friend asks only for your time and not your money.";

    // Spacer for between numbers
    var space = ('      ')

    // Get random lucky numbers
    var number1 = Math.floor(Math.random() * 60 + 1);
    var number2 = Math.floor(Math.random() * 60 + 1);
    var number3 = Math.floor(Math.random() * 60 + 1);
    var number4 = Math.floor(Math.random() * 60 + 1);
    var number5 = Math.floor(Math.random() * 60 + 1);
    var number6 = Math.floor(Math.random() * 60 + 1);

    // Get a number for picking the quote
    var rand_int = Math.floor(Math.random() * 5);

    // Show the quote & number
    document.getElementById("fortuneArea").innerHTML = (quotes[rand_int] + "<br />" + "Lucky numbers:" + space + "<b>" + number1 + space + number2 + space + number3 + space + number4 + space + number5 + space + number6 + "</b>");

  } // end function
b {
  color: #FF0000;
  font-weight: normal;
}
<form action="">
  <p>
    <input type="button" value="Show my fortune!" onclick="showFortune();" />
  </p>
</form>
<div id="fortuneArea"></div>

7 个答案:

答案 0 :(得分:5)

你可以改变这个:

// Get random lucky numbers
var number1 = Math.floor(Math.random() * 60 + 1);
var number2 = Math.floor(Math.random() * 60 + 1);
var number3 = Math.floor(Math.random() * 60 + 1);
var number4 = Math.floor(Math.random() * 60 + 1);
var number5 = Math.floor(Math.random() * 60 + 1);
var number6 = Math.floor(Math.random() * 60 + 1);

对此:

var numbers = new Array();
for(var i = 0; i < 6; i++)
{
    numbers.push((Math.floor(Math.random() * 60 + 1)));
}

答案 1 :(得分:2)

重新说一下......你想要1到60之间的6个幸运数字。

不是存储在6个独立变量中,而是将em粘贴到数组中。

var lucky = [];
for (var l = 0; l < 6; l++) {
    lucky.push(Math.floor(Math.random() * 60 + 1));
}

var lucky = new Array(6);
for (var l = 0; l < lucky.length; l++) {
    lucky.push(Math.floor(Math.random() * 60 + 1));
}

应该做的伎俩。另外,我知道你没有问,但是

var rand_int = Math.floor(Math.random() * 5);

会好很多。

var rand_int = Math.floor(Math.random() * quotes.length);

这样,如果/当您添加更多引号时,您不必重做代码。保持卡车运输的朋友,编码很有趣。

- 修改

哎呀,忘了输出。而不是

number1 + space + number2

进行加入。 lucky.join('')将为您提供您正在寻找的内容。

答案 2 :(得分:1)

&#13;
&#13;
function showFortune() {
    // Quotes to grab from array.
    var quotes = new Array(5)
    quotes[0] = "The road to riches is paved with homework.";
    quotes[1] = "Ignore your previous fortune.";
    quotes[2] = "Avoid taking unnecessary gambles.";
    quotes[3] = "You love peace.";
    quotes[4] = "A friend asks only for your time and not your money.";

    // Spacer for between numbers
    var space = ('      ')

    var numbers ="";
    // Get random lucky numbers
    for (i=0;i<6;i++)
    {
      numbers+= Math.floor(Math.random() * 60 + 1) + space;
    }

    // Get a number for picking the quote
    var rand_int = Math.floor(Math.random() * 5);

    // Show the quote & number
    document.getElementById("fortuneArea").innerHTML = (quotes[rand_int] + "<br />" + "Lucky numbers:" + space + "<b>" + numbers+ "</b>");

  } // end function
&#13;
<div id="fortuneArea"></div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您的代码可以在多个位置进行优化。让我们一点一点地进行:

首先,JavaScript中的注释会显示//标记,而非<!-- -->。后者用于HTML。 ; - )

使用这样的多个空间绝不是一件好事。使用CSS。一个好方法是把你的&#34;数字&#34;在span元素中。

生成数字是重复的,所以我们可以循环它。让我们现在一起做所有这些:

//We will fill this variable with the text of numbers.
var numberString = "";

//We're gonna loop 6 times: from 0 to 5.
for (int i = 0; i < 6; i++)
{
   var number = Math.floor(Math.random() * 60 + 1); 
   numberString += "<span>" + number + "</span>";
}

//Quote number:
var rand_int = Math.floor(Math.random()*5); 

//And now everything together:
document.getElementById("fortuneArea").innerHTML=(quotes[rand_int]+"<br />"+"<span>Lucky numbers:</span><b>" + numberString + "</b>");

只剩下要做的就是在你的css中添加:

span {
    margin-right: 10px;
}

答案 4 :(得分:0)

默认情况下,您可以首先将字符串绑定到数组,而不是单独绑定。

var quotes = ["The road to riches is paved with homework.", "Ignore your previous fortune.", "Avoid taking unnecessary gambles.", "You love peace.", "A friend asks only for your time and not your money."];

然后你要创建一个数组,循环(我设置数字在&lt; 6,所以0,1,2,3,4,5)一组数字,然后将项目推入数组。

// Get random lucky numbers
var numbers = [];
for(i = 0; i < 6; i++) {
    numbers.push(Math.floor(Math.random() * 60 + 1));
}

答案 5 :(得分:0)

对于幸运数字功能,您需要检查重复项

function showFortune() {

    var quotes = ["The road to riches is paved with homework.",
                  "Ignore your previous fortune.",
                  "Avoid taking unnecessary gambles.",
                  "You love peace.",
                  "A friend asks only for your time and not your money."];

    var numbers = [];

    var counter = 0;

    do 
    {
        var num = Math.floor(Math.random() * 60 + 1);

        if (numbers.indexOf(num) == -1) // if the array doesn't already contain number
        {
            // add the unique number to the array
            numbers.push(num);
            counter++; 
        }

    } while (counter < 6);


    var output = ""; // Build html string from numbers

    for(var i = 0; i < numbers.length; i++)
    {
        output += "<span>" + numbers[i] + "</span>"
    }

    var rand_int = Math.floor(Math.random() * 5);

    // Show the quote & number
    document.getElementById("fortuneArea").innerHTML = 
       (quotes[rand_int] + "<br />" + "Lucky numbers:" + output);
}

在CSS中设置间距

span {
    padding-left: 10px;
    paddding-right: 10px;
}

这将有助于浏览器正确显示它。

在IE 8及BTW以下不支持array.indexOf

答案 6 :(得分:0)

另一种方式:

function showFortune(){
    var quotes = [
            "The road to riches is paved with homework.",
            "Ignore your previous fortune.",
            "Avoid taking unnecessary gambles.",
            "You love peace.",
            "A friend asks only for your time and not your money."
        ],
        // Use an element to which you can give a CSS width
        space = '<i class="spacer"></i>',
        i = 6,
        // Use the 'rand()' function created below
        result = rand(quotes) + '<br />' + 'Lucky numbers:' + '<b>';

        while(i--){ result += space + rand(60); }

        document.getElementById("fortuneArea").innerHTML = result + '</b>';
}

function rand(param){
    if(param.length){ return param[ Math.floor(Math.random() * param.length) ]; }
    return Math.floor(Math.random() * param + 1);
}
b { color: #FF0000; font-weight: normal; } .spacer{ display: inline-block; width: 1.5em; }
<form action="">
  <p>
    <input type="button" value="Show my fortune!" onclick="showFortune();" />
  </p>
</form>
<div id="fortuneArea"></div>

相关问题