0

I made a function to check if a number is a prime number or not, if not, then it returns the number, otherwise(if it is a prime number) it returns 0.

var maybePrimeNumber = [5, 8, 11, 14, 17, 20, 23, 28, 31]; //numbers to test

function isNotPrime(integer){
    var prime = true;            //start off assuming that it is a prime nr for now
    var returnNumber = 0;        //to be returned at the end

    for(i = 2; i<integer; i++){  //go from 2 up to the number,
        if(integer % i === 0){   //if the number is divisible by any nr between 2 and itself, 
            prime = false;       //then say that it isn't a prime number
        }
    }

    if(!prime){                  //if it isn't a prime number, then
        returnNumber = integer;  //prepare to send back the number
    }

    return returnNumber;
}

document.getElementById("demo").innerHTML = maybePrimeNumber.reduce(
    (total, integer) => {return total + isNotPrime(integer)}
); 
//sum all non-prime numbers up from the array and put the sum into the paragraph "demo"

And the problem is as follows:

When I run this, it gives me 75, but it should be giving me 70.

So when I replace the last line with this:

document.getElementById("demo").innerHTML = isNotPrime(maybePrimeNumber[0]);

To check if it mistakenly calculates 5 to be a non-prime number - it returns 0, so it doesn't miscalculate.

Why does it give the sum 75 instead of 70 then??

1
  • 2
    In your reduce call, specify the 2nd parameter, which is the initial value of total - to be 0 - else it'll be the first number in the array, which is 5, hence your 5 off answer. See: jsfiddle.net/88gx55tm Commented Feb 24, 2017 at 20:28

2 Answers 2

5

You should always include an initial value for reduce.

document.getElementById("demo").innerHTML = maybePrimeNumber
  .reduce((total, integer) => {return total + isNotPrime(integer)}, 0);

The first value for total is the first element of the array, if no initial value is included, so 5 is always included in the sum.

Sign up to request clarification or add additional context in comments.

Comments

0

The following function works and is cleaner. Note that naming convention would suggest a function names isSomething returns a boolean. (Note I used a console.log since I tested this in node).

const maybePrimeNumber = [5, 8, 11, 14, 17, 20, 23, 28, 31]; //numbers to test
function isNotPrime(integer) {
  for(i = 2; i < integer / 2; i++){ //go from 2 up to the number,
    if(integer % i === 0){    //if the number is divisible by any nr between 2 and itself, 
      return true;          //then say that it isn't a prime number
    }
  }

  return false;
}

console.log(maybePrimeNumber.reduce((total, integer) => total + (isNotPrime(integer) ? integer : 0), 0));

As @Xufox points out in his answer, your reduction is actually the issue as you should supply an initial value (0) as the second argument to .reduce(); otherwise the first value in the array is considered the initial total value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.