Sum All Primes Intermediate Algorithm Scripting Free Code Camp

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


Fan funding goes towards buying the equipment necessary to deliver 4k videos, 4k webcam, and a high quality microphone better audio. Any support is very appreciated!

My channel is here for aspiring programmers to learn easier and help debug any issues from the many great free resources available on the web.

Check out my other videos going over HTML, CSS, Ruby, Ruby on Rails, Java, JavaScript, Python, PHP, SQL, Command Line, BootStrap, jQuery, and WordPress from CodeCademy, CodeCombat, FreeCodecamp and more!

-~-~~-~~~-~~-~-
Please watch: "How I Became a Developer | My Developer Journey of the Last 3 Years | Ask a Dev"
-~-~~-~~~-~~-~-
Рекомендации по теме
Комментарии
Автор

You are God sent since I started following you my life has changed for the better keep up the good work you are a blessing to us here

winiswamtshingwana
Автор

I did a little console logging to make this all a bit clearer -- here were my results which show what is divisible (and therefore not a prime number)

"this is i: 2"
"and this is j: 2"
2 is prime
"this is i: 3"
"and this is j: 2"
"and this is j: 3"
3 is prime
"this is i: 4"
"and this is j: 2" * break
"this is i: 5"
"and this is j: 2"
"and this is j: 3"
"and this is j: 4"
"and this is j: 5"
5 is prime
"this is i: 6"
"and this is j: 2" * break
"this is i: 7"
"and this is j: 2"
"and this is j: 3"
"and this is j: 4"
"and this is j: 5"
"and this is j: 6"
"and this is j: 7"
7 is prime
"this is i: 8"
"and this is j: 2" * break
"this is i: 9"
"and this is j: 2"
"and this is j: 3" * break

jacklyons
Автор

cool, best explanation i've managed to find out there ;)

danutzz
Автор

Your explanation really helped me tremendously especially how array.reduce works. Thank you very much. I went over your way about five times to make sure I really got it into my head.

BLloyd
Автор

Good walkthrough. I really needed help figuring out identifying the primes. I needed to work it out on paper before I completely understood the nested for loops. A few attempts crashed my browser.

ShelJones
Автор

that makes it much easier to understand. thanks

christianrodier
Автор

Certainly a good piece of coding. Thought it just got a little confusing. I think creating an isPrime function and then throwing that in an if-condition in the main function for loop really makes it seem a little easier. Just that moment where you felt you needed a second to explain seemed as if abstracting the "isPrime" would be easier to go with. Sorry if this is coming off as aggressively critical. Love all the help you give.

murghay
Автор

I struggled on this challenge, after a while came up with this (not ideal for high values but passes this test just fine):

function sumPrimes(num) {

// test for primacy

function primacy(test){
for (var i = 2 ; i < test ; i++){
if (test % i === 0){
return false;
}
}
return true;
}

var arr = [2];

// iterate from 3 until num (and test)

for (var prime = 3 ; prime <= num ; prime += 2){
if (primacy(prime) === true) {
arr.push(prime);
}
}

// add the array together

var answer = arr.reduce(function(a, b){
return a + b;
});

return answer;

}

kimonushki
Автор

I didn't want to use reduce either. I just added each prime iteration to var sum = 0 in the first if statement.
if (i===j){
sum = sum+i;
}
if (i%j===0){
break;
}

CashBarnes
Автор

Is it bad or unspeakable to use for loop for adding? Becouse that's how I did it.

DupaCycDupa
Автор

Good stuff.. I did it a different way but Im wondering how your break line works.
Wouldnt it break on the first non prime number?

chriscullen
Автор

what if i is 4 and j is also 4... both the conditions are met then why isnt 4 been pushed to the array

sahil
Автор

I can't figure out why the for loops work :/

arletbode
Автор

Amazing explanation. I just don't really get the logic behind: (i===j)

davidobaro