Forbes magazine logo Ranked Best Coding Bootcamps 2023

Potential infinite loop while num > 700

Altcademy Team wrote on 7 February 2018

potential infinite loop while num > 700. https://freecodecamp.cn/challenges/sum-all-primes
function sumPrimes(num) { var arr = []; var ifTrue = 1; for (var i = 2; i < num; i ++) { ifTrue = 1; for (var j = 2; j < i; j ++) { if (i % j === 0) { ifTrue = 0; } } if (ifTrue == 1) { arr.push(i); } } num = arr.reduce(function(a,b) { return a + b ; }); return num; }

Hi @Lijianliang1997, as your code wasn't really working. I made slight modification to it and is now working.
function sumPrimes(num) { var arr = []; for (var i = 2; i <= num; i ++) { var divisible = false; for (var j = 2; j < i; j ++) { if (i !== j && i % j === 0) { divisible = true; } } if (divisible === false) { arr.push(i); } } num = arr.reduce(function(a,b) { return a + b ; }); return num; } sumPrimes(1000); //=> 76127


The key was this if statement
if (i !== j && i % j === 0) { divisible = true;}

You only divide if you are not dividing it by itself. Other wise you will get 0 since 2%2 => 0

Trusted by

Students and instructors from world-class organizations

Imperial College London
Carnegie Mellon University
City University of Hong Kong
Hack Reactor
Cisco Meraki
University of Oxford
Swift
Bazaarvoice
Waterloo
Uber
AtlanTech
Tumblr
Boston College
Bombardier Aerospace
University of St. Andrews
New York University
Minerva Schools at KGI
Merrill Lynch
Riot Games
JP Morgan
Morgan Stanley
Advanced Placement®
Google
KPMG
The University of Hong Kong
University of Toronto
SCMP
Moat
Zynga
Hello Toby
Deloitte
Goldman Sachs
Yahoo
HSBC
General Assembly
Tesla
McGill University
Microsoft

Join the upcoming Cohort #89

Enroll for May 6th, 2024