Advertisement
Guest User

Untitled

a guest
May 1st, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. <script src="fizzbuzz.js" charset="utf-8" defer="defer"></script>
  2.  
  3. var fizzContainer = $("div.number-container");
  4.  
  5. var divisor1 = 3,
  6. divisor2 = 5,
  7. maxFizz = 100,
  8. word1 = "Fizz",
  9. word2 = "Buzz",
  10. windowWidth = window.innerWidth,
  11. minNumOfColumns = 1,
  12. maxNumOfColumns = 12,
  13. widthPerColumn = 150,
  14. columns,
  15. linesPerColumn,
  16. columnSizingParameters = {};
  17.  
  18. calculateHeading(windowWidth);
  19. calculateColumns();
  20.  
  21. $(window).resize(function(){
  22. windowWidth = window.innerWidth;
  23. calculateHeading(windowWidth);
  24.  
  25. $(".number-container").replaceWith("<div class='number-container'></div>");
  26. calculateColumns();
  27. });
  28.  
  29. function calculateHeading(windowSize) {
  30. if(windowSize <= 500) {
  31. $("h1").attr("class", "transition xSmallHeading");
  32. } else if (windowSize <= 700) {
  33. $("h1").attr("class", "transition smallHeading");
  34. } else if (windowSize <= 1000) {
  35. $("h1").attr("class", "transition mediumHeading");
  36. } else if (windowSize >= 1000) {
  37. $("h1").attr("class", "transition largeHeading");
  38. }
  39. }
  40.  
  41. function calculateColumns() {
  42. var columnIt = minNumOfColumns;
  43. for (columnIt; columnIt <= maxNumOfColumns; columnIt++) {
  44. columnSizingParameters[columnIt] = columnIt * widthPerColumn;
  45. }
  46.  
  47. for (var i = 1; i <= maxNumOfColumns; i++) {
  48. if ((windowWidth >= columnSizingParameters[i]) && (windowWidth <= columnSizingParameters[i + 1])) {
  49. columns = i;
  50. }
  51. }
  52. linesPerColumn = Math.ceil(maxFizz / columns);
  53. createNumbers();
  54. }
  55.  
  56. function createNumbers() {
  57. var i = 1;
  58.  
  59. for (var c = 1; c <= columns; c++) {
  60. var col = $('<div></div').appendTo(".number-container");
  61. col.addClass('column');
  62.  
  63. for (var a = 1; a <= linesPerColumn && i <= maxFizz; a++, i++) {
  64. if ((i % divisor1 === 0) && (i % divisor2 === 0)) {
  65. createContentLine(word1 + word2, "double-match", col, i, true);
  66. continue;
  67. } else if (i % divisor1 === 0) {
  68. createContentLine(word1, "match-1", col, i, true);
  69. continue;
  70. } else if (i % divisor2 === 0) {
  71. createContentLine(word2, "match-2", col, i, true);
  72. continue;
  73. } else {
  74. createContentLine(i, "num", col, i, false);
  75. continue;
  76. }
  77. }
  78. }
  79. }
  80.  
  81. function createContentLine(toPrint, cssClass, col, i, appendNumToWord) {
  82. var contentLine = $("<p>" + toPrint + "</p>").appendTo(col);
  83. contentLine.addClass(cssClass);
  84.  
  85. if (appendNumToWord) {
  86. var appendedNumber = $("<span></span").appendTo(contentLine);
  87. appendedNumber.text(" • " + i).addClass("num");
  88. }
  89. }
  90.  
  91. <div class="container">
  92.  
  93. <h1>The fizzbuzz test...in Javascript!</h1>
  94.  
  95. <div class="number-container">
  96.  
  97. </div>
  98. </div>
  99.  
  100. * {
  101. padding: 0px;
  102. margin: 0px;
  103. box-sizing: border-box;
  104. }
  105.  
  106. .container {
  107. width: auto;
  108. height: 100%;
  109. display: block;
  110.  
  111. }
  112.  
  113. h1 {
  114. display: block;
  115. background-color: black;
  116. color: white;
  117. width: 100%;
  118. font-family: 'Berkshire Swash', cursive;
  119. }
  120.  
  121. .transition {
  122. transition: 500ms;
  123. }
  124.  
  125. /* Screen width Less than 500px */
  126. .xSmallHeading {
  127. font-size: 25px;
  128. line-height: 25px;
  129. padding: 15px;
  130. }
  131. /* Less than 700px */
  132. .smallHeading {
  133. font-size: 32px;
  134. line-height: 32px;
  135. padding: 20px;
  136. }
  137.  
  138. /* Less than 1000px */
  139. .mediumHeading {
  140. font-size: 40px;
  141. line-height: 40px;
  142. padding: 30px;
  143. margin-bottom: 10px;
  144. }
  145.  
  146. /* Greater than 1000px */
  147. .largeHeading {
  148. font-size: 50px;
  149. line-height: 50px;
  150. padding: 40px;
  151. margin-bottom: 20px;
  152. }
  153.  
  154. .number-container {
  155. display: flex;
  156. margin-top: 20px;
  157. margin-bottom: 20px;
  158. text-align: center;
  159. line-height: 30px;
  160. width: 100%;
  161. height: calc(100% - 150px);
  162. }
  163.  
  164. .column {
  165. flex-grow: 1;
  166. display: inline-block;
  167. border-right: 1px solid lightblue;
  168. }
  169.  
  170. .column:last-of-type {
  171. border-right: none;
  172. }
  173.  
  174. .num {
  175. color: #585858;
  176. font-weight: bold;
  177. font-style: normal;
  178. font-family: sans-serif;
  179. font-size: 12px;
  180. }
  181.  
  182. .double-match {
  183. font-weight: 600;
  184. font-size: 20px;
  185. color: #3f89d6;
  186. font-family: 'Crimson Text', serif;
  187. }
  188.  
  189. .match-1, .match-2 {
  190. font-family: 'Josefin Slab', serif;
  191. font-weight: 700;
  192. font-size: 18px;
  193. }
  194.  
  195. .match-1 {
  196. color: #7ac962;
  197. }
  198.  
  199. .match-2 {
  200. color: #ba62d4
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement