sumPrimes math issue

sumPrimes math issue
0

#1

I’m not thinking I should post my actual code here but I’ll explain my issue well enough. I’ve written a solution for this challenge in the intermediate algorithms section. Now when I run this code in node, on repl.it, or on codepen it returns the correct result. When run from FCC it returns differing and incorrect results. I’m not using global variables so the variable should not be retaining it’s value between function calls. Again I am getting the correct results in other places. Can anyone understand what is happening?


#2

When you say it “returns differeing and incorrect results,” what exactly do you mean. Are you looking at the returned value and seeing something different than you expect? Or are you just seeing tests fail? If you’re just seeing failing tests, you may be running into FCC’s infinite loop protection which causes your code to terminate early.


#3

I’m seeing tests fail and I’m seeing a different result every time. It’s just weird that the same code doesn’t have any issues anywhere else. No infinite loop or wrong results


#4

If you’re seeing your code return different results every time, then it definitley sounds like there’s something wrong with your logic. (I can only guess, since you haven’t shared your code.)

Just as a reminder, the infinite loop protection is based on a timer. If your code is inefficient, it will trigger the protection even if you do not actually have an infinite loop.


#5

Should I share the code here? Isn’t it strange that it works in all other environments? If the timer is set to stop it in milliseconds than maybe.


#6

That would help us help you better.
Just make sure you wrap it inside the[spoiler]This text will be blurred[/spoiler] tag.


#7

function sumPrimes(num){
let result = 0;
for (let counter = 2; counter <= num; counter++) {
let prime = true;
for (let i = 2; i <= counter; i++) {
if (counter%i===0 && i!==counter) {
prime=false;
}}
if (prime === true) {
// console.log(counter);
result+=counter;
}
}
return result;
}


#8

Anyone? Why would it execute differently on FCC than anywhere else. It works fine on node, jsfiddle, repl.it, and codepen. It doesn’t even take a second to finish. Could there be some JS in the FCC page interfering with my solution??


#9

The code you pasted above passes the Free Code Camp tests.


#10

See… I thought it should. It just won’t seem to on any of my computers. I’ve tried brave and firefox browsers. I’ll try chrome next. Thank you for confirming what I thought.


#11

And it worked in Chrome. Thanks for everyones time.


#13

Worked in Chrome for me. Now I find that especially confusing that it didn’t work in Chrome for you. There’s something erratic happening. I don’t know how to begin to find the bug. Hopefully it gets found though. FCC is the best learn to code resource I’ve ever used.


#14

Without any information about how it was failing, there’s not a lot that can be figured out.


#15

also confused
but not surprised it only works in chrome
is this the code you used to get it to pass?

function sumPrimes(num) {
  let result = 0;
  for (let counter = 2; counter <= num; counter++) {
    let prime = true;
    for (let i = 2; i <= counter; i++) {
      if (counter % i === 0 && i !== counter) {
        prime = false;
      }
    }
    if (prime === true) {
      result += counter;
    }
  }
  return result;
}

sumPrimes(10);

#16

copy pasta strait into vscode

looks like a bug to me
op was telling the truth


#17

Yes. It appears to be the same. I had similar issues with the next challenge as well.


#18

Well I’m certainly willing to help find any bugs or issues here. Just let me know how I can provide you with the information necessary and I’ll be glad to pass it along if you’d like.