Sum All Primes -- JavaScript -- Free Code Camp

preview_player
Показать описание

Function hoisting. Writing modular code by breaking out logic into separate functions.
Рекомендации по теме
Комментарии
Автор

The most clear explanation I've watched, THANK YOU SIR I GUESS YOU'RE A SENIOR NOW IT'S BEEN A YEARS

icaruz
Автор

This is BY FAR the best explanation I've found for this issue. Thank you!

munikun
Автор

I struggled with this one, even after watching other video tutorials. Then when I watched yours, you made it so easy to understand the logic. Thank you so much!

Steveopr
Автор

This is what I was searching for.... That for loop returning true and false values were not so clear in any of the tutorials.. you made it super simple.. great work.. I am a new subscriber of yours..

JD-hqkn
Автор

and you do more than just leading towards solution: good coding practices are also present. Thanks a lot !!

BENJO
Автор

I was messing around with a double for loop and couldn't quite get it right (knew what needed to happen, but wasn't able to make it work). As soon as you mentioned the function inside the if-statement it became much clearer. Thanks once again!

xTobesx
Автор

You are awesome, Stephen. Really love how well you explain the code. Thanks for your good work.

yozmari
Автор

wow... i finally realized how the solution actually works perfectly. man.... a prime number can only return false - your code --> "if num%x===0 { return false}" if it % 1 OR ITSELF. But you put x start from 2, so it won't modulus 1. then u put "x<num" so it won't modulus itself....GENIUS! 😂😂😂

lycan
Автор

Thank you so much for the video. I code everything in the for loop and I end up getting confused. That was a good tip of writing an individual function.

edgarpan
Автор

Started watching your videos a week or so ago and they have helped me a lot. I was pretty stuck on the first few challenges in the intermediate algorithm section, but after watching your videos I have really started to understand some of the concepts I just wasn't getting before and I've been able to complete most of the challenges on my own, then use your videos to see how I can improve my code.

I think I may have run into a bug on FCC though, I just couldn't get this one to pass, and even with copying your exact code (code below) it doesn't work. The first two tests pass, but when it tries to do 977 it returns that it might be an infinite loop.

[EDIT]: I was able to pass by using //noprotect on the firstline - to override the "potential infinite loop on line 7"

However, I ran the code in codepen using 977 and it returned exactly what it should have, so the code works, there must just be something up with FCC right now, so I can't pass the challenge - unless I'm missing something?? Here's the code I tried:


function sumPrimes(num) {

var range = [];

for (var i = 2; i <= num; i++) {
if (isPrimeNumber(i)) {
range.push(i);
}
}

return range.reduce(function(a, b) {
return a + b;
});
}

function isPrimeNumber(num) {
for (var x = 2; x < num; x++) {
if (num % x === 0) {
return false;
}
}
return true;
}

console.log(sumPrimes(977));

derekcommonground
Автор

Quick question, I tried to test a few larger numbers, seems the FCC page prompts a potential infinite loop after a specific time maybe? it's funny because when I paused the playback on your video it would allow me to get to higher numbers, so I'm guessing it has to do with a timer in Chrome or the app on FCC and when my memory is freed up I can go up higher...that sound about right? I've gotten to 10K no problem now, but was getting shut down at 9650 while watching a youtube video. Could only get around 9637 while watching youtube.

markclynch
Автор

what really confused me at the beginning was because u put "num" as the parameter of function that u created to check primes. since the original function has "num" as parameter already.

lycan
Автор

So basically to check whether a number is a prime or not is to use THAT NUMBER and modulus every number below it and MAKE SURE every calculation HAS A REMAINDER. just don't modulus/divide 1 or the number itself because it will end up with NO remainder.

lycan
Автор

why the code breaks if line 16 is less than and equal to num instead of less than 16 only:
for ( ... ; x <= num; ...)

I can't reason out why there is empty array error:
TypeError: Reduce of empty array with no initial value

evanz
Автор

Thanks for making this video, really explained a lot :)

Just a question though, how come you added all of the primes to an array and then reduced it at the end? Why not start with the range as 0 and then just 'range += i' to update the total? Then you can just put 'return range;' at the end.

I tried it on FCC and it worked fine, just wanted to make sure I wasn't missing something. Thanks again

Nymik
Автор

Thank you for these tutorials, keep up the good work! Do you work as a developer? I'm doing my best to become a web dev since I really want to get into the industry...Free Code Camp is great but sometimes I feel like I'm cheating when I need help from walkthroughs :/

webnguitarsadf
Автор

Here is my solution:

function sumPrimes(num) {

var counter = 0;
var sumOfPrimes = 0;

// Iterate through num
for (var i = 2; i <= num; i++) {
// Iterate through all dividers of each 'i'
for (var j = 1; j <= i; j++) {
/* Counting all dividers ('j') of 'i',
remainders of which are absent */
if (i % j === 0) {
counter += 1;
}
}
/* If 'i' is a prime number, it has only two
dividers without remainder: 1 and itself */
if (counter === 2) {
// Sum all primes
sumOfPrimes += i;
}
// Reset counter for the next i-iteration
counter = 0;
}

return sumOfPrimes;
}

BENJO
Автор

I think we all know who the other guy is. And I have to agree with you all. This is so much clearer. I am delaying on the weather app. It seem a little complicated to me. and everything now is https. so all the tutorials need a little tweaking.

zootechdrum
Автор

i still don't understand... the sum of the first 10 prime numbers is not 17. i'm confused

ziatco
Автор

maybe you can skip the reduce part and just create a sum variable that will sum every time checkPrime(i)==true { sum += i;}. Hope it helps :)

kle