Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function primeNonprime(input) {
- let command = input.shift();
- let primeSum = 0;
- let nonPrimeSum = 0;
- while (command != "stop"){
- let num = Number(command);
- if (num < 0) {
- console.log("Number is negative.");
- command = input.shift();
- continue;
- }
- let isPrime = true;
- if (num == 1) {
- isPrime = false;
- } else if (num == 2){
- isPrime = true;
- } else {
- for (let i = num; i >= 2; i--) {
- if (num % i == 0 && i != num) {
- isPrime = false;
- break;
- }
- }
- }
- if (isPrime) {
- primeSum += num;
- } else {
- nonPrimeSum += num;
- }
- command = input.shift();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement