Advertisement
Guest User

Untitled

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