Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. // you can write to stdout for debugging purposes, e.g.
  2. // console.log('this is a debug message');
  3.  
  4. function solution(N) {
  5. var max = 0,
  6. count = false,
  7. zeros = 0;
  8.  
  9. while (N >= 1) {
  10. if (N % 2 === 0) {
  11. if (count) zeros += 1;
  12. }
  13. else {
  14. if (zeros > max) {
  15. max = zeros;
  16. }
  17. zeros = 0;
  18. count = true;
  19. }
  20. N = parseInt(N / 2);
  21. }
  22.  
  23. return max;
  24. }
  25.  
  26. // test result
  27. var results = {
  28. total: 0,
  29. bad: 0
  30. };
  31.  
  32. // function to test function
  33. function test(functionToTest, args, expected) {
  34. results.total++;
  35. var result = functionToTest.apply(this, args);
  36. if (result !== expected) {
  37. results.bad++;
  38. console.log("Expected " + expected + ", but was " + result);
  39. }
  40. }
  41.  
  42. // show results function
  43. function showTestResults() {
  44. console.log("Of %d tests, %d failed, %d passed.", results.total, results.bad, (results.total - results.bad));
  45. }
  46.  
  47. // test start
  48. test(solution, [9], 2);
  49. test(solution, [529], 4);
  50. test(solution, [20], 1);
  51. test(solution, [15], 0);
  52. test(solution, [1041], 5);
  53. test(solution, [0], 0);
  54. test(solution, [1], 0);
  55. test(solution, [66561], 9);
  56. test(solution, [74901729], 4);
  57. test(solution, [100000], 4);
  58. test(solution, [51712], 2);
  59.  
  60. // show results
  61. //showTestResults();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement