Guest User

Untitled

a guest
Jul 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. // tickets are lucky when sum of first half equals sum of second half
  2. const isLucky = n => {
  3. // min is 10 and we know it's false
  4. if (n === 10) return false;
  5. // store digits in an array
  6. const digitCorral = [];
  7. // use modulo, division and Math.trunc
  8. while (n > 0) {
  9. // push reverses the order but that doesn't matter
  10. // since the sum of each half will still be the same
  11. digitCorral.push(n % 10);
  12. n /= 10;
  13. n = Math.trunc(n);
  14. }
  15. // get sum of first half of array
  16. const firstHalf = digitCorral
  17. .slice(0, digitCorral.length / 2)
  18. .reduce(function(a, b) {
  19. return a + b;
  20. });
  21. // get sum of second half of array
  22. const secondHalf = digitCorral
  23. .slice(digitCorral.length / 2, digitCorral.length)
  24. .reduce(function(a, b) {
  25. return a + b;
  26. });
  27. // return a boolean
  28. return firstHalf === secondHalf;
  29. };
Add Comment
Please, Sign In to add comment