需要随机颜色的动态表

| 我需要做一些家庭作业,相对简单...。 我需要动态获取表格中每个表格的随机颜色和随机数... 现在,我的问题是一个奇怪的问题... 有总和变量\'s,我给他们一个数字,并期望id做些简单的事情,例如sum = 4 + 2 他给我的结果是42而不是6 ... 这是我的代码,我希望有人能帮助我。
<script type=\"text/javascript\" language=\"javascript\">
function buildTable(){
   // gets the number that inserted to the text box
   rows=document.getElementById(\"txtNumber\").value;

   // alert error masssege
   // if    (1): the textBox(txtNumber)is empty
   // or if (2): the value in the text box is not a number
   // or if (3): the value in the text box is not between 1-15
   if (rows==\"\" || !IsNumeric(rows) || rows<1 || rows>15)
      alert(\"invalid number!\");
   else{
      ////////////////////// Add your code here ////////////////////// 

      //varables
      randomNumbers=new Array();
      var index=0;

      for(var h=0;h<(rows*4)-4;h++){
         randomNumbers[h]=Math.floor(Math.random()*101);
      }

      //here start printing table + print first line to the html document
      document.write(\"<table>\");
      getFirstrow(randomNumbers,rows,index);
      index=rows;
      //print table with out first or last row
      for(var i=0;i<rows-2;i++){
         var newColor=randomColor();
         document.write(index+\" \");
         document.write(\"<tr>\");
         document.write(\"<td style=background-color:\"+newColor+\">\"+randomNumbers[index]+ \"</td>\");
         for(var j=0;j<rows-2;j++){
            document.write(\"<td>\"+ \"< src=\'face.png\' />\"+ \"</td>\");
         }
         newColor=randomColor();
         document.write(\"<td style=background-color:\"+newColor+\">\" + randomNumbers[index+1]+ \"</td>\");
         document.write(\"</tr>\");
         index=index+2;
      }//end of for

      index=(rows*3)-4;
      //print last row of table
      getLastrow(randomNumbers,rows,index);
      document.write(\"</table>\");
      getSum(randomNumbers);
   }//End of Else
}//end of buildTable

//////////////////////////////////////function get first  row//////////////
function  getFirstrow(randomNumbers,rows,index){
   document.write(\"<tr>\");
   for(var j=index;j<index+rows;j++){
      newColor=randomColor();
      document.write(\"<td style=background-color:\"+newColor+\">\"+ randomNumbers[j]+ \"</td>\");
   }
   document.write(\"</tr>\");
   // return  randomNumbers;
}//end of function

function  getLastrow(randomNumbers,row,index){
   a=(row+(row-2)*2);
   sum=0;
   sum=index+row;

   document.write(\"<tr>\");
   for( j=index;j<sum;j++){
      newColor=randomColor();
      document.write(\"<td style=background-color:\"+newColor+\">\"+ randomNumbers[j]+ \"</td>\");
      document.write(\"<p style=color:blue>\"+j+\"</p>\") ;

      document.write(sum+\" \");
   }

   document.write(\"</tr>\");

   // return  randomNumbers;
}//end of function 

//////////////////////////////////////function that gets random color//////////////////
function getDecimalColor(colorList){
   newColor=\"#\";
   for(x in colorList){
      switch(colorList[x]){
      case 10:colorList[x]=\"AA\";break;
      case 11:colorList[x]=\"BB\";break;
      case 12:colorList[x]=\"CC\";break;
      case 13:colorList[x]=\"DD\";break;
      case 14:colorList[x]=\"EE\";break;
      case 15:colorList[x]=\"FF\";break;
      default:colorList[x]=colorList[x].toString()+ colorList[x].toString();
      }
      newColor+=colorList[x];
   }//end of for
   return newColor;
}//END OF FUNCTION 

///////////////////////////function for random color//////////////////
function randomColor(){
   var color=new Object();
   var colorarray=new Array();
   color.r=Math.floor(Math.random()*16);
   color.g=Math.floor(Math.random()*16);
   color.b=Math.floor(Math.random()*16);
   colorArray=new Array(color.r,color.g,color.b);
   color.decimalcolor=getDecimalColor;

   newColor = color.decimalcolor(colorArray);
   return newColor;
}//end of random color

///////////////////////////get random numbers///////////////////////////
function getRandomNumbers(){
   return Math.floor(Math.random()*101)
}

//////////////////////////get sum of all the random number////////////////////////////////////
function getSum(randomNumbers){
   var sum=0;
   for(x in randomNumbers){
      sum+= randomNumbers[x]; 
   } 
   alert(\"the sum is: \"+sum);  
}

// help function that checks if a text is a number
function IsNumeric(sText){
   var ValidChars = \"0123456789.\";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++){
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
      }
   }
   return IsNumber;
}//end of is numeric
</script>
已邀请:
+运算符取决于数据类型-在您的情况下,它意味着字符串连接。 您必须使用parseInt()或(value * 1)将用过的变量转换为数字(我想是int)
function getSum(randomNumbers)
{                   
   var sum=0;
   for(x in randomNumbers)
   {
      sum+= parseInt(randomNumbers[x]); 
   } 
   alert(\"the sum is: \"+sum);  
}
我假设您正在谈论的部分代码如下:
sum = index+row;
尝试这个:
sum = parseInt(index) + parseInt(row);

要回复问题请先登录注册