Checkerboard Patterns by using Js

How to get Checkerboard Patterns like in the example??

Page 1 of 1

1 Replies - 1481 Views - Last Post: 27 July 2008 - 04:10 PM

#1 AsOman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-July 08

Checkerboard Patterns by using Js

Post icon  Posted 27 July 2008 - 02:37 PM

Hi every body,
My question is :
I've to create a checkerboard type pattern out of asterix. So I've a line of 8 asterix ("* ") and a space between them (" "). Then it goes to the next row which is indented to make the checkboard effect.
I want the result to be like this:
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
by using while structure and only three statements which are:
document.write( "* ");


document.write( " " );

document.writeln();
In fact I get inly one row so what can I add to get the other rows???

Here is my code:
[code]
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- Fig. 7.14: increment.html -->
<!-- Preincrementing and Postincrementing. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Preincrementing and Postincrementing</title>
<script type = "text/javascript">
<!--
var row = 0;
cols = 0;

while(row <= 7 )

{

document.write( "* ");


document.write( " " );

document.writeln();
row++;

}


// -->
</script>
</head><body></body>
</html>
[code]
Thank you alot.

Is This A Good Question/Topic? 0
  • +

Replies To: Checkerboard Patterns by using Js

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Checkerboard Patterns by using Js

Posted 27 July 2008 - 04:10 PM

You either have to do embeded loops or multiply columns needed by rows needed and then do an if statement to output a line break after every 8 times through the loop.

Here is something that may helps:

<script type = "text/javascript">
<!--
var row = 0;
var cols = 0;

while(row <= 7 ){
  while(cols <= 7){
	document.write( "* ");
	cols++;
  }
  document.write("<br>");
  cols = 0;
  row++;
}
// -->
</script>


Hope that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1