Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Exercise 5.3
- Chess Board Pattern using loops
- This program creates a chess board pattern on screen.
- -Creates two functions called for creating each line of the chess board,
- ■ == Black (is a black box character
- □ == White (is a white box character)
- Fucntion B for placing black
- Function W for placing white
- Declare size variable, which allows for quick customization of board size.
- Declare Output variable for what should be displayed by the system
- Use a For loop to add the correct combination of characters per line.
- One loop would be for one batter, which will be pattern A (black first)
- Pattern B will mean white first.
- We have multiple lines. So, two line functions.
- Pattern A
- Pattern B
- */
- //Define Functions
- function White() {
- output += "□"; //Creates a white space
- }
- function Black() {
- output += "■"; //Creates a black space
- }
- function PatternA(){
- //For loop that creates a line using PatternA
- for(i = 0; i <size; i++) {
- Black();
- White();
- }
- }
- function PatternB(){
- //For loop that creates a line using PatternB
- for(i = 0; i <size; i++) {
- White();
- Black();
- }
- }
- //Declares the size variable, which is uses to determine board length/width. Actual result is multiplied by two!
- var size = 4;
- //Creates the output variable, which starts as a blank string.
- var output = "";
- //Loop that creates the chess board using nested functions/loops
- for(j = 0; j <size; j++) {
- PatternA();
- output += "<br/>";
- PatternB();
- output += "<br/>";
- }
- //Actually displays ouput
- document.getElementById("first_line").innerHTML = output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement