首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要随机颜色的动态表

需要随机颜色的动态表
EN

Stack Overflow用户
提问于 2011-04-19 20:58:28
回答 2查看 816关注 0票数 0

我需要做一些相对简单的作业...

我需要到动态表,得到的随机颜色和每个表中的随机数字…我的问题是个奇怪的问题。

这是我的代码,希望有人能帮助我..

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2011-04-19 21:11:58

+运算符取决于数据类型-在你的例子中,它意味着字符串连接。您必须使用parseInt()(value * 1)将使用的变量转换为number (我假设是int

代码语言:javascript
复制
function getSum(randomNumbers)
{                   
   var sum=0;
   for(x in randomNumbers)
   {
      sum+= parseInt(randomNumbers[x]); 
   } 
   alert("the sum is: "+sum);  
}
票数 1
EN

Stack Overflow用户

发布于 2011-04-19 21:06:59

我假设您正在讨论的代码部分如下所示:

代码语言:javascript
复制
sum = index+row;

试试这个:

代码语言:javascript
复制
sum = parseInt(index) + parseInt(row);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5716709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档