Bug at Sum All Primes Challenge?

Bug at Sum All Primes Challenge?
0.0 0

#1

Hello coders!

I am trying to solve challenge with Prime Numbers" Intermediate Algorithm Scripting: Sum All Primes".

my code is:

function sumPrimes (num){
var arrOfNum = createArrOfNum(num);
var arrOfPrimes = [];
for (var i = 0; i < arrOfNum.length; i++){
var numToTest = arrOfNum[i];
arrOfPrimes.push(isAprimeNum(numToTest));
}
var sumP = sum(arrOfPrimes);
var num = sumP - 1; // while 1 is not a Prime Number
return (num);
}

function createArrOfNum (num){
var arrOfNum = [];
for (var i = 1; i <= num; i++){
	arrOfNum.push(i);
}
return arrOfNum;
}

function isAprimeNum(num){
var arrOfTestNum = createArrOfTestNum(num);
var flag = true;
	for (var k = 0; k < arrOfTestNum.length; k++) {
	var remainder = num % arrOfTestNum[k];
		if (remainder == 0){
    	flag = false;
    	}
	}
if (flag == true){
return num;
}else{
return 0;
}
}

function createArrOfTestNum (num){
var arrOfTestNum = [];
var lastTestNum = num - 1;
for (var j = 2; j <= lastTestNum; j++){
	arrOfTestNum.push(j);
}
return arrOfTestNum;
}

function sum (arrOfPrimes){
var sum = 0;
for (var j = 0; j < arrOfPrimes.length; j++){
sum += arrOfPrimes[j];
}
return sum;
}

I wrote the code at js.do . When I tested it at js.do, everything was ok.
When I tested it at freeCodeCamp with the number 977 the test failed.

Where is my mistake?

Is it problem of new FCC platform?

Honestly these updates are not user-friendly.

I am looking for prompt help.

sincerely yours,
Maria


#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Damn… that’s a big code. Anyway, your code evaluates to correct answer, the only problem is that you might be using Firefox, try Chrome it will pass the test.


#4

While your code does work, it will not pass the FCC tests, because your algorithm is not as efficient as it could be. The FCC tests have a built-in feature which protects against potential infinite loops. Your code does not have an infinite loop, but the tests think so, because your solution is slower than expected.

Try to rethink your algorithm so that takes fewer iterations to solve the challenge.