Guest User

Untitled

a guest
Jul 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. // noprotect
  12. // ^^ `noprotect` is here to prevent a bug with jsbin and for loops.
  13.  
  14.  
  15. function fizzBuzz(countTo) {
  16. var result = []
  17. for (var i=1; i <= countTo; i++) {
  18. if (i % 15 === 0) {
  19. result.push('fizzbuzz');
  20. }
  21. else if (i % 5 === 0) {
  22. result.push('buzz');
  23. }
  24. else if (i % 3 === 0) {
  25. result.push('fizz');
  26. }
  27. else {
  28. result.push(i);
  29. }
  30. }
  31. return result;
  32. }
  33. }
  34.  
  35.  
  36.  
  37.  
  38. /* From here down, you are not expected to
  39. understand.... for now :)
  40.  
  41.  
  42. Nothing to see here!
  43.  
  44. */
  45.  
  46.  
  47.  
  48. // tests
  49. (function testFizzBuzz() {
  50. // we'll use the variables in our test cases
  51. var countTo = 16;
  52. var expected = [
  53. 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz',
  54. 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16
  55. ];
  56.  
  57. var actual = fizzBuzz(countTo) || [];
  58.  
  59. if (
  60. expected.length === actual.length &&
  61. expected.every(function(item, index) {
  62. return actual[index] === item;}) ) {
  63.  
  64. console.log('SUCCESS: fizzBuzz is working');
  65. }
  66. else {
  67. console.log('FAILURE: fizzBuzz is not working');
  68. }
  69. })();
  70. </script>
  71.  
  72.  
  73.  
  74. <script id="jsbin-source-javascript" type="text/javascript">// noprotect
  75. // ^^ `noprotect` is here to prevent a bug with jsbin and for loops.
  76.  
  77.  
  78. function fizzBuzz(countTo) {
  79. var result = []
  80. for (var i=1; i <= countTo; i++) {
  81. if (i % 15 === 0) {
  82. result.push('fizzbuzz');
  83. }
  84. else if (i % 5 === 0) {
  85. result.push('buzz');
  86. }
  87. else if (i % 3 === 0) {
  88. result.push('fizz');
  89. }
  90. else {
  91. result.push(i);
  92. }
  93. }
  94. return result;
  95. }
  96. }
  97.  
  98.  
  99.  
  100.  
  101. /* From here down, you are not expected to
  102. understand.... for now :)
  103.  
  104.  
  105. Nothing to see here!
  106.  
  107. */
  108.  
  109.  
  110.  
  111. // tests
  112. (function testFizzBuzz() {
  113. // we'll use the variables in our test cases
  114. var countTo = 16;
  115. var expected = [
  116. 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz',
  117. 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16
  118. ];
  119.  
  120. var actual = fizzBuzz(countTo) || [];
  121.  
  122. if (
  123. expected.length === actual.length &&
  124. expected.every(function(item, index) {
  125. return actual[index] === item;}) ) {
  126.  
  127. console.log('SUCCESS: fizzBuzz is working');
  128. }
  129. else {
  130. console.log('FAILURE: fizzBuzz is not working');
  131. }
  132. })();</script></body>
  133. </html>
Add Comment
Please, Sign In to add comment