Javascript生成随机数字随机数?

时间:2018-04-12 04:55:41

标签: javascript arrays

我正在为类做一个简单的任务,我正在尝试使用Javascript创建一个表。我完全设置了我的表,只是每隔一行显示一个随机数,然后跳到下一行。我该如何摆脱随机排?

 var table = ["Name", "Turn Average", "Surface Area", "100% Boosting"];

 var names = ["Octane", "Dominus", "Breakout", "X-Devil", "Batmobile"];

 var turnaverages = [2.18, 2.22, 2.22, 2.21, 2.25];

 var surfacearea = [34495, 34529, 32679, 34242, 34365];

var maxboost = [1.967, 2.031, 2.035, 2.014, 2.10];

function info() {
document.write("<table>"); 
document.write("<tr>"); 

//these next lines output the table heading (th) tags for the table (this information doesn't repeat in the table)
document.write("<th>Name</th>");
document.write("<th>Turn Average</th>");
document.write("<th>Surface Area</th>");
document.write("<th>100% Boosting</th>");
document.write("</tr>"); //close first table row element
//create a for loop that will last for the number of days in the forecast
for(var i = 0; i < names.length; i++){
    document.write("<tr>"); //create a table row
        //output each table data tag for the table with information pulled 
         from the arrays
        document.write("<td>" + names[i] + "</td>");
        document.write("<td>" + turnaverages[i] + "</td>");
        document.write("<td>" + surfacearea[i] + "</td>");
        document.write("<td>" + maxboost[i] + "</td>");
        document.write("</tr>");
        //depending on the description of the weather for the day, change 
           the image to be representative to that description -- (for 
           example on a rainy day, show the rainy image)
        if ( names[i] === "Dominus" ) {
            document.write("<td>" + turnaverages[i] + surfacearea[i] + 
            maxboost[i] + "</td>");
        }
        else if ( names[i] === "Breakout" ){
            document.write("<td>" + turnaverages[i] + surfacearea[i] + 
             maxboost[i] + "</td>");
        }
            else if ( names[i] === "X-Devil" ){
            document.write("<td>" + turnaverages[i] + surfacearea[i] + 
            maxboost[i] + "</td>");
        }
        else if( names[i] === "Batmobile" ){
            document.write("<td>" + turnaverages[i] + surfacearea[i] + 
            maxboost[i] + "</td>");

        }

       document.write("</tr>"); //close the table row element
     }
      document.write("</table>"); //close the table element
    }

1 个答案:

答案 0 :(得分:0)

你有

document.write("<td>" + maxboost[i] + "</td>");
document.write("</tr>");

紧接着是尝试写td s的天气测试,然后是另一个

document.write("</tr>"); //close the table row element

因此,您只需删除第一个</tr>,因为您实际上并不想关闭该行:

&#13;
&#13;
var table = ["Name", "Turn Average", "Surface Area", "100% Boosting"];

var names = ["Octane", "Dominus", "Breakout", "X-Devil", "Batmobile"];

var turnaverages = [2.18, 2.22, 2.22, 2.21, 2.25];

var surfacearea = [34495, 34529, 32679, 34242, 34365];

var maxboost = [1.967, 2.031, 2.035, 2.014, 2.10];

function info() {
  document.write("<table>");
  document.write("<tr>");

  //these next lines output the table heading (th) tags for the table (this 
  document.write("<th>Name</th>");
  document.write("<th>Turn Average</th>");
  document.write("<th>Surface Area</th>");
  document.write("<th>100% Boosting</th>");
  document.write("</tr>"); //close first table row element
  //create a for loop that will last for the number of days in the forecast
  for (var i = 0; i < names.length; i++) {
    document.write("<tr>"); //create a table row
    //output each table data tag for the table with information pulled 
    document.write("<td>" + names[i] + "</td>");
    document.write("<td>" + turnaverages[i] + "</td>");
    document.write("<td>" + surfacearea[i] + "</td>");
    document.write("<td>" + maxboost[i] + "</td>");
    //depending on the description of the weather for the day, change 
    if (names[i] === "Dominus") {
      document.write("<td>" + turnaverages[i] + surfacearea[i] +
        maxboost[i] + "</td>");
    } else if (names[i] === "Breakout") {
      document.write("<td>" + turnaverages[i] + surfacearea[i] +
        maxboost[i] + "</td>");
    } else if (names[i] === "X-Devil") {
      document.write("<td>" + turnaverages[i] + surfacearea[i] +
        maxboost[i] + "</td>");
    } else if (names[i] === "Batmobile") {
      document.write("<td>" + turnaverages[i] + surfacearea[i] +
        maxboost[i] + "</td>");

    }

    document.write("</tr>"); //close the table row element
  }
  document.write("</table>"); //close the table element
}
info();
&#13;
&#13;
&#13;

2.22345292.031和类似的数字是字符串共处的结果 - 当你接受一个字符串并+时,你将得到另一个字符串。 (不知道你真正想要的是什么)