desislava_topuzakova

03. Sum Prime Non Prime

Apr 10th, 2021
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   let index = 0;
  3.   let command = input[index];
  4.  
  5.   let primeSum = 0;
  6.   let notPrimeSum = 0;
  7.  
  8.   while (command != "stop") {
  9.     let num = Number(command);
  10.  
  11.     if (num < 0) {
  12.       console.log("Number is negative.");
  13.     } else if (num === 1) {
  14.       primeSum += num;
  15.     } else {
  16.       let count = 0;
  17.       for (let i = 1; i <= num; i++) {
  18.         if (num % i === 0) {
  19.           count++;
  20.         }
  21.       }
  22.  
  23.       if (count === 2) {
  24.         primeSum += num;
  25.       } else {
  26.         notPrimeSum += num;
  27.       }
  28.     }
  29.  
  30.     command = input[++index];
  31.   }
  32.  
  33.   console.log(`Sum of all prime numbers is: ${primeSum}`);
  34.   console.log(`Sum of all non prime numbers is: ${notPrimeSum}`);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment