Advertisement
Spocoman

03. Sum Prime Non Prime

Dec 30th, 2021 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function  sumPrimeNonPrime(input) {
  2.     let prime = 0;
  3.     let nonPrime = 0;
  4.     let index = 0;
  5.  
  6.     while (input[index] !== "stop") {
  7.         let num = Number(input[index++]);
  8.         if (num >= 0) {
  9.             if (num % 2 === 0 || num % 3 === 0 || num % 5 === 0) {
  10.                 if (num === 2 || num === 3 || num === 5 || num === 7) {
  11.                     prime += num;
  12.                 } else {
  13.                     nonPrime += num;
  14.                 }
  15.             } else {
  16.                 prime += num;
  17.             }
  18.         } else {
  19.             console.log("Number is negative.");
  20.         }
  21.     }
  22.     console.log(`Sum of all prime numbers is: ${prime}`);
  23.     console.log(`Sum of all non prime numbers is: ${nonPrime}`);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement