Advertisement
Guest User

task

a guest
Jan 30th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Task description */
  2. /*
  3.     Write a function that finds all the prime numbers in a range
  4.         1) it should return the prime numbers in an array
  5.         2) it must throw an Error if any on the range params is not convertible to `Number`
  6.         3) it must throw an Error if any of the range params is missing
  7. */
  8.  
  9. function solve() {
  10.  
  11.     function isPrime(number) {
  12.  
  13.         for (let i = 2; i < number; i += 1) {
  14.             if (number % i == 0) {
  15.                 return false;
  16.             }
  17.         }
  18.         return true;
  19.     }
  20.  
  21.     return function findPrimes(start, end) {
  22.  
  23.         if (start == null || end == null) {
  24.             throw '';
  25.         } else if (isNan(start) || isNan(end)) {
  26.             throw '';
  27.         } else {
  28.             var array = [];
  29.  
  30.             for (let i = +start; i <= +end; i += 1) {
  31.                 if (i != 0 && i != 1) {
  32.                     if (isPrime(i)) {
  33.                         //console.log(i);
  34.                         array.push(i);
  35.                     }
  36.                 }
  37.             }
  38.  
  39.             return array;
  40.         }
  41.     }
  42. }
  43.  
  44. solve();
  45. module.exports = solve;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement