Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. 'use strict';
  2.  
  3. /**
  4. * @param {number} num
  5. * @returns {*} Will return 'fizzbuzz'/'buzz'/'fizz' or the number
  6. */
  7. function fizzbuzz(num) {
  8. if (num % 15 === 0) return 'fizzbuzz';
  9. if (num % 5 === 0) return 'buzz';
  10. if (num % 3 === 0) return 'fizz';
  11. return num;
  12. }
  13. /**
  14. * Builds the HTML for the FizzBuzz output
  15. * @param {*} fizzResult Will be 'fizzbuzz'/'buzz'/'fizz' a number
  16. * @returns {string} Generates ONE fizzbuzz item
  17. */
  18. function generateFizzHtml(fizzResult) {
  19. let fizzClass = '';
  20. if (typeof fizzResult === 'string') {
  21. fizzClass = fizzResult;
  22. }
  23. return `
  24. <div class="fizz-buzz-item ${fizzClass}">
  25. ${fizzResult}
  26. </div>
  27. `;
  28. }
  29. /**
  30. * Handles the form submission
  31. */
  32. function handleSubmit() {
  33. $('#number-chooser').on('submit', event => {
  34. event.preventDefault();
  35. const countTo = $('#number-choice').val();
  36. // clear user value
  37. $('#number-choice').val('');
  38. // capture results in an array
  39. const fizzBuzzResults = [];
  40. // push the result of the fizzbuzz function
  41. // being run on the param: all numbers from 1
  42. // to the number entered in the form
  43. for (let i = 1; i <= countTo; i++) {
  44. fizzBuzzResults.push(fizzbuzz(i));
  45. }
  46.  
  47. const html = fizzBuzzResults.map(result => generateFizzHtml(result));
  48. $('.js-results').html(html);
  49. });
  50. }
  51.  
  52. $(handleSubmit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement