desislava_topuzakova

03. Sum Prime Non Prime

Apr 10th, 2021
703
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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×