Sum All Primes - Intermediate Algorithm Scripting - Free Code Camp

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Thank you so much Mr. Ian another important lesson that I am learning from you.
function sumPrimes(num) {
if (num <= 1) {
return "Numbers lower than 0 don't work here"
}

let counter = 2;
let sum = 0;

while (counter <= num) {
if (isPrime(counter)) {
sum += counter;
}
counter += 1;
}

return sum;
}

function isPrime(singleNumber) {
let counter = 2;
while (counter < singleNumber) {
if (singleNumber % counter === 0) {
return false;
}
counter += 1;
}
return true;
}

let result = sumPrimes(977);
console.log(result);
console.log(isPrime(977))

zken
Автор

Thanks so much for posting your videos, very helpful!

mattlambert
Автор

hi Sir! I'm new in programming and I find your videos very helpful. only there are some parts that I dont understand. Is the counter = 2 constant? please help

caypanganiban
Автор

i am watching tutorials in every problem, how do I learn. I worked on it for like 3 hours but couldn't get it

Rakibuddin-tgfr
Автор

could you explain what the "sum +=counter" part please!

cleaningbad
Автор

i didnt understand which value does it have the singleNumber in the isPrime function :/ can someone help me?

jordimantilla
Автор

i know all basics, but couldn't think the logic. HOW TO THINK LIKE THIS??!!!

vetri
Автор

function sumPrimes(num) {
let sum =0;
for (let i=2;i<=num;i++){
let n=0;
let x=0;
while(n<i){
n++;
if(i%n===0){
x++
};
}
if(x==2){
sum+=n;
}
}
return sum;
}

falafel