Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. function bearTrap(L, D, X) {
  2.  
  3. var ans,
  4. xes = [];
  5.  
  6.  
  7.  
  8. if (typeof(L) != 'number' || typeof(D) != 'number' || typeof(X) != 'number') {
  9. console.log('L, D and X should be numbers')
  10. return;
  11. }
  12.  
  13.  
  14.  
  15. for (var i = L; i <= D; i++)
  16. isSumOf(i, X) ? xes.push(i) : '';
  17.  
  18. function isSumOf(n, x) {
  19. var chars = n.toString().split(''),
  20. sum = 0;
  21.  
  22. chars.forEach(function(el, i) {
  23. sum += +el;
  24. });
  25.  
  26. if (sum === x)
  27. return true;
  28.  
  29. else
  30. return false;
  31. }
  32.  
  33.  
  34.  
  35. var min = Math.min.apply(null, xes),
  36. max = Math.max.apply(null, xes);
  37.  
  38. ans = [min, max];
  39.  
  40.  
  41.  
  42. return ans;
  43.  
  44. }
  45.  
  46. var answer = bearTrap(0, 10000, 24);
  47.  
  48. console.log(answer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement