PrimeNumber Tango

PARTIAL SPOILER ALERT !

OK It seems that it is missing a comma or semicolon somewhere… but wondered if I could have someone else look at this for me…

function sumPrimes(num) {
 let y;
 let x=[];
  
  for (let p=0; p<=num; p++){
    let notPrime=false;
  for (let c=2; c<=p; c++){

   if(p%c===0 && c!==p){
    notPrime=true ;
      }
     }
   if (notPrime === false) {
        x.push(p); 
    }
      
   }
  
  
   console.log(x) ;
  y=x.reduce(function(total,amt) {
    return total + amt;
  }                
  
}  
  

sumPrimes(10);

Thank s in advance.

Do you just need an else statement?

Do you really need to check if it is divisible by each number? This wouldn’t work for the last few tests as they would time out. You would save a lot of computational power if you checked just a subset of those numbers. You can check some prime numbers theory, or just ask.

@ilenia In the prime number theory, a number which is not divisible by any other number save for 1 and itself. ( of course current discussions have changed on that matter and dropped 0 and/or 1 depending on the circles you run in ) . At least it has been true since I was in elementary school. One could say that after a certain point the multiplication of numbers would far exceed the value of NUM … Its too early in the morning for me to try to figure out where that point would be as a mathmatical relationship to NUM .

OF course, part of the problem here is not whether or not I am checking too many numbers, but the point in question for me, is " what (comma/ semi-colon/ bracket etc) did I forget or move that this thing is throwing syntax errors for me ?

To be clear I would be open to being wrong on multiple points… my feelings is not tied to being right etc… and primarily , thank you for answering my query !

Well, one of your method is missing the closing ) after arguments list

Here where:

Do you mean like this?
I am using this tool, it does usually a good job in telling me syntax errors and if I need to come up with a less resource intensive algorithm: http://pythontutor.com/javascript.html#mode=edit

Correct

But if you know that your number is not divisible by 2 and 3, do you need to check also if it is divisible by 4, 6, 8, 9, 12, 16, 18… or you know already that it is not?

That is a way to make it less resource intensive

your code id blurred LOL

That is the [spoiler][/spoiler] tag

If you want the solution you can tap/click on it…

Thank you for the website… that is helpful indeed ! I have put that into my favorites… Its much more helpful than the responses I have gotten from CodePen which is what I have been using …

OH Drrrrrrrrrrr OK so I need to stick a break in there… Ok… Pre-caffine brain… thank you

OK so I did not stick the breaks in. for the computational settings… However, It passed the first 2 tests, but failed the 3rd test, although, ( at least in code pen) the calculation completes correctly for the sumPrimes(977) should return 73156. test… ??

Here was the printout from the console in codepen :
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977]

73156

Note that I removed the console.log of X when in the actual FCC editor…

It didnt hang… gave the correct answer but still failed ?

Have you changed anything else other than fixed the missing bracket?

Your original function needs to much time to execute, too many steps (consider this, 233 steps for primes below 10, says the tutor)
It triggers the infinite loop protection and is stopped prematurely

You need to remove redundant steps
For example, other than the advice above, if a number is not prime you can avoid checking if it is divisible by other numbers (break the loop)

The one other thing I changed was that I made both the starting integers a 2 in order to remove 0 and 1 from the array. ( apperantly they did not want/need those and it was causing an incorrect response… Here is the updated code. :

function sumPrimes(num) {
 let y;
 let x=[];
  
  for (let p=2; p<=num; p++){
    let notPrime=false;
  for (let c=2; c<=p; c++){

   if(p%c===0 && c!==p){
    notPrime=true ;
      } 
     }
   if (notPrime === false) {
        x.push(p); 
    }
      
   }
  
  
   
  y=x.reduce(function(total,amt) {
    return total + amt;
  }  );              
  
 console.log(y);
  
   }
sumPrimes(977);

Yeah, it is the infinite loop protection

Try calling the function inside the tutor with sumPrimes(977) instead of sumPrimes(10)
And tweak it till you can go to visualising the execution

Well in an interesting twist of events. I changed the code and shaved enough cycles off the code to pass the FCC editor and challenge. BUT the editor website you shared with me now says that I still have too many execution cycles. Its good enough for me… I will come back to it at some point … I just want to get to the next challenge , get through the projects and complete this cert, because I have another project I want to build for my portfolio and its been on the plate for FAR too long LOL

Thank you for your help with this and for the website. that is nice !
(((((Bearhugs)))))

1 Like