Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /*Exercise 5.3
  2.  
  3. Chess Board Pattern using loops
  4.  
  5.  
  6. This program creates a chess board pattern on screen.
  7.  
  8. -Creates two functions called for creating each line of the chess board,
  9.  
  10. ■ == Black (is a black box character
  11. □ == White (is a white box character)
  12.  
  13. Fucntion B for placing black
  14. Function W for placing white
  15.  
  16. Declare size variable, which allows for quick customization of board size.
  17. Declare Output variable for what should be displayed by the system
  18.  
  19. Use a For loop to add the correct combination of characters per line.
  20.  
  21. One loop would be for one batter, which will be pattern A (black first)
  22. Pattern B will mean white first.
  23.  
  24. We have multiple lines. So, two line functions.
  25. Pattern A
  26. Pattern B
  27. */
  28.  
  29. //Define Functions
  30.  
  31. function White() {
  32. output += "□"; //Creates a white space
  33. }
  34.  
  35. function Black() {
  36. output += "■"; //Creates a black space
  37.  
  38. }
  39. function PatternA(){
  40. //For loop that creates a line using PatternA
  41. for(i = 0; i <size; i++) {
  42. Black();
  43. White();
  44. }
  45. }
  46. function PatternB(){
  47. //For loop that creates a line using PatternB
  48. for(i = 0; i <size; i++) {
  49. White();
  50. Black();
  51. }
  52. }
  53. //Declares the size variable, which is uses to determine board length/width. Actual result is multiplied by two!
  54. var size = 4;
  55.  
  56. //Creates the output variable, which starts as a blank string.
  57. var output = "";
  58.  
  59. //Loop that creates the chess board using nested functions/loops
  60. for(j = 0; j <size; j++) {
  61. PatternA();
  62. output += "<br/>";
  63. PatternB();
  64. output += "<br/>";
  65. }
  66.  
  67. //Actually displays ouput
  68. document.getElementById("first_line").innerHTML = output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement