簡體   English   中英

需要隨機顏色的動態表

[英]dynamic table that needs random colors

我需要做一些家庭作業,比較簡單。

我需要動態獲取表中每種顏色的隨機顏色和隨機數的動態表...現在,我的問題是一個奇怪的問題...

有求和變量,我給他們一個數字,並期望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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM