2016-03-26 6 views
-2

こんにちは、私は2色のテーブルを作成するタスクを持って、最初のものは次のようになります。Javascriptの大学タスク

enter image description here

と私は、スクリプトを経由して、それを完了した:

n =10; 
document.write("<table>"); 
for (i =1; i <n; i++) 
{ 
    document.write("<tr>"); 
    for (j =1; j <=n; j++) 
    { 
     if (j <=i) 
      s ="class ='r1'"; 
     else 
      s ="class ='r2'"; 
     document.write("<td " + s + "> </td>"); 
    } 
    document.write("</tr>"); 
} 
document.write("</table>"); 

2番目は次のようになります。

enter image description here

と私はそれを行う方法がわかりません。助言がありますか?

+0

この1つはまた、動作します: 'もし((J + I + 1)%3 == 1)カウンターがあることができ'またはそのとにかくこのタスクを処理するキーは3で除算を使用することです – fires3as0n

答えて

0

パターンは簡単です - 3セルごとにが紫色です。パターンはまた次の行に直接続くので、k % 3を使用できます。kは、すでに描画されたセルの総数です。

+0

直接コードで別の答えがありますが、あなたの方法はどのように動作するかを記述していますので、解決策として選択します。返答したすべての人 – fires3as0n

2

これはトリック

n = 10; var c=1; 
 
document.write("<table>"); 
 
for (i =1; i <=n; i++) 
 
{ 
 
    document.write("<tr>"); 
 
    for (j =1; j <=n; j++) 
 
    { 
 
     c += 1; 
 
     if ((c % 3) == 0) 
 
      s ="class ='r1'"; 
 
     else 
 
      s ="class ='r2'"; 
 
     document.write("<td " + s + "> </td>"); 
 
    } 
 
    document.write("</tr>"); 
 
} 
 
document.write("</table>");
table td{ 
 
    width: 25px; 
 
    height: 25px; 
 
} 
 
.r1{ 
 
    background-color: pink; 
 
} 
 
.r2{ 
 
    background-color: blue; 
 
}

を行います
+0

はい、これは、ありがとうございます。 – fires3as0n

関連する問題