Advertisement
Guest User

Untitled

a guest
May 26th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Make Your Own Bingo Card</title>
  5. <link href="Style/bingo.css" rel="stylesheet" />
  6. <script type="text/javascript">
  7. //This contains the variables for the number of squares and max number. This is done so we can easily alter then.
  8. //EG, if we were doing a VAT, we would have a variable for VAT rather than type 20% each time.
  9. //This way if VAT changed again we would have to change one line rather than multiple.
  10. var model =
  11. {
  12. NumOfSquares: 24,
  13. MaxNumber: 75,
  14. };
  15.  
  16. //this contains the code to generate the random number.
  17. var helpers =
  18. {
  19. //this function uses max as a paramater. So whatever the value of maxNumber gets passed into it. When it's called in view.
  20. RandomNumber: function (max)
  21. {
  22. //Math.random() generates a random number between 0 and 1. We then multiple it by the max number (75).
  23. //this then rounds the number down to an interger. It Then adds one because we rounded down.
  24. //If we didn't add one, the number generated would be between 0 and 74 rather than 1 and 75.
  25. return Math.floor(Math.random() * max) + 1;
  26. }
  27. }
  28.  
  29. var view =
  30. {
  31. //sets the prefix to "square" and creates the text for the error message.
  32. prefix: "square",
  33. errorMessage: "Sorry you are unable to play this game",
  34. //this function has thisSquare as a paramater.
  35. setSquare: function (thisSquare)
  36. {
  37. //creates currSquare and sets it to prefix, so "square" and then concatanates it with the id of thisSquare, which would be i from controller.
  38. //so it's the same as var currSquare = "square" + i; from version 2.
  39. var currSquare = view.prefix + thisSquare.toString();
  40. //Changes the inner html of the current square to the contents of RandomNumber passing in the maxNumber from model.
  41. document.getElementById(currSquare).innerHTML = helpers.RandomNumber(model.MaxNumber);
  42. },
  43. //This function will display the error message when called.
  44. displayMessage: function (message)
  45. {
  46. //alert will make a popup with
  47. message
  48. alert(message);
  49. },
  50. };
  51.  
  52. var controller =
  53. {
  54. initAll: function ()
  55. {
  56. //This if statement checks if we can use getElementById. If we can't it will display the error message.
  57. if (document.getElementById)
  58. {
  59. //this for loop creates a var called i, sets it to 0.
  60. //as long as i is less than the number of squares (24), it runs setsquare in view, then adds one to i.
  61. //so it will repeat 24 times, populating the contents of the squares.
  62. for (var i = 0; i < model.NumOfSquares; i++)
  63. {
  64. //runs setSqaure with i being passed in.
  65. view.setSquare(i);
  66. }
  67. }
  68. else
  69. {
  70. //displays the error message created in view.
  71. view.displayMessage(view.errorMessage);
  72. }
  73. }
  74. };
  75. //runs initALL inside controller when the window loads.
  76. window.onload = controller.initAll;
  77.  
  78. </script>
  79. </head>
  80. <body>
  81. <h1>Bingo</h1>
  82. <div>
  83. <table>
  84. <tr>
  85. <th>B</th>
  86. <th>I</th>
  87. <th>N</th>
  88. <th>G</th>
  89. <th>O</th>
  90. </tr>
  91. <tr>
  92. <td id="square0">&nbsp;</td>
  93. <td id="square5">&nbsp;</td>
  94. <td id="square10">&nbsp;</td>
  95. <td id="square14">&nbsp;</td>
  96. <td id="square19">&nbsp;</td>
  97. </tr>
  98. <tr>
  99. <td id="square1">&nbsp;</td>
  100. <td id="square6">&nbsp;</td>
  101. <td id="square11">&nbsp;</td>
  102. <td id="square15">&nbsp;</td>
  103. <td id="square20">&nbsp;</td>
  104. </tr>
  105. <tr>
  106. <td id="square2">&nbsp;</td>
  107. <td id="square7">&nbsp;</td>
  108. <td id="free">Free</td>
  109. <td id="square16">&nbsp;</td>
  110. <td id="square21">&nbsp;</td>
  111. </tr>
  112. <tr>
  113. <td id="square3">&nbsp;</td>
  114. <td id="square8">&nbsp;</td>
  115. <td id="square12">&nbsp;</td>
  116. <td id="square17">&nbsp;</td>
  117. <td id="square22">&nbsp;</td>
  118. </tr>
  119. <tr>
  120. <td id="square4">&nbsp;</td>
  121. <td id="square9">&nbsp;</td>
  122. <td id="square13">&nbsp;</td>
  123. <td id="square18">&nbsp;</td>
  124. <td id="square23">&nbsp;</td>
  125. </tr>
  126. </table>
  127. </div>
  128. <div>
  129. <p>
  130. <button id="BtnCall">Shout Number</button>
  131. </p>
  132.  
  133. <div id="usednum"></div>
  134.  
  135. </div>
  136. </body>
  137. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement