Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. const assert = require('assert');
  2.  
  3. const isPrime = (n) => {
  4. if (n <= 1) return false
  5. else if (n <= 3) return true
  6. else if (n % 2 === 0 || n % 3 === 0) return false
  7.  
  8. let i = 5;
  9. while (i * i <= n) {
  10. if(n % i === 0 || n % (i + 2) === 0) return false
  11. i += 6
  12. }
  13. return true;
  14. }
  15.  
  16. const count = (array, n) => {
  17. const filtered = array.filter(a => a === n);
  18. console.log(n, filtered, filtered.length);
  19. return filtered.length;
  20. }
  21.  
  22. const filter = (fst, snd) => {
  23. return fst.reduce((acc, a) => {
  24. if(isPrime(count(snd, a)))
  25. return acc
  26.  
  27. return acc.concat(a);
  28. }, [])
  29. }
  30.  
  31. const a = [2,3,9,2,5,1,3,7,10]
  32. const b = [2,1,3,4,3,10,6,6,1,7,10,10,10]
  33. const c = [2,9,2,5,7,10]
  34. assert.deepEqual(filter(a, b), c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement