- I've wasted my afternoon assuming this paragraph is factual, it's not.. It maybe a decent reference point to get the idea, but after spending all day trying to code it into an exercise, I started to catch several anomalies, starting with 31, the digits still add up to 4, but it is also prime.

As an educator, please quantify partial information.

Divisibility: Know your divisibility rules.

Try 2: Any number ending in 2 is an even number and it will be divisible by 2, therefore it is not prime.

Try 3. Take the number, and add the digits up, when those digits are divisible by 3, the number is not prime. Take 2469, those digits add up to 21, and 21 is divisible by 3, therefore 2469 is not a prime number.

Try 4. Take the last 2 digits of the number, is it divisible by 4? Then the number is divisible by 4. Try 8336, the last 2 digits are 36 which are divisible by 4, therefore 8336 is not prime.Try 5: any number ending in 0 or 5 is divisible by 5 and not prime.

175 lines of code.. just to find out the logic is incomplete, I've written complete Simon Games in half that. It was a nice challenge, but the more I had to track down bugs, the more I realized it wasn't the programming that was flawed.

//helper functions

function isItEven(n) {

// Try 2: is an even number and it will be divisible by 2, therefore it is not prime.

if (n === 1) {

return;

}

if (n === 2) {

return n;

}

if (n % 2 !== 0) {

console.log(n);

return n;

}

} //end of isItEven

function isItTHREED(n) {

// https://www.thoughtco.com/how-to-determine-number-is-prime-2312518

if (n === 3) {

return n;

}

// Try 3. Take the number, and add the digits up, when those digits are divisible by 3, the number is not prime.

// Take 2469, those digits add up to 21, and 21 is divisible by 3, therefore 2469 is not a prime number.

console.log("3s", n);

n = n.toString();

var digitONE = Number(n.charAt(0));

var digitTWO = Number(n.charAt(1));

var digitTHREE = Number(n.charAt(2));

var digitFOUR = Number(n.charAt(3));

console.log("digits", digitONE);

console.log("digits", digitTWO);

console.log("digits", digitTHREE);

console.log("digits", digitFOUR);

var testing = digitONE + digitTWO + digitTHREE + digitFOUR;

console.log(testing);

n = Number(n);

if (testing % 3 !== 0) {

return n;

}

} //end of isItTHREED

function isItFOURED(n) {

// https://www.thoughtco.com/how-to-determine-number-is-prime-2312518

var fourTrap = [];

n = n.toString();

var digitLAST = Number(n.charAt(n.length));

var digitNEXTtoLast = Number(n.charAt(n.length - 1));

console.log("digits", digitLAST);

console.log("digits", digitNEXTtoLast);

var testing = digitLAST + digitNEXTtoLast;

console.log(testing);

n = Number(n);

if (testing % 4 === 0) {

fourTrap.push(n);

console.log("fourThrap", fourTrap);

}

if (testing % 4 !== 0) {

return n;

}

} //end of isItFOURED

function isItFIVED(n) {

if (n === 5) {

return n;

}

var fifthTrap = [];

var fifthLast = n.toString();

console.log(n);

var digitLAST = Number(fifthLast.charAt(fifthLast.length - 1));

var testing = digitLAST;

console.log("testLOG", testing);

n = Number(n);

if (testing === 5 || testing === 0) {

fifthTrap.push(n);

console.log("traptFIVE", fifthTrap);

}

if (testing !== 5 && testing !== 0) {

return n;

}

} //end of isItFIVED

function isItSeveneD(n) {

// Try 2: is an even number and it will be divisible by 2, therefore it is not prime.

if (n === 7) {

return n;

}

if (n % 7 !== 0) {

console.log(n);

return n;

}

} //end of isItSeveneD

function isItElevened(n) {

// Try 2: is an even number and it will be divisible by 2, therefore it is not prime.

if (n === 11) {

return n;

}

if (n % 11 !== 0) {

console.log(n);

return n;

}

} //end of isItElevened

function isIt13th(n) {

// Try 2: is an even number and it will be divisible by 2, therefore it is not prime.

if (n === 13) {

return n;

}

if (n % 13 !== 0) {

console.log(n);

return n;

}

} //end of isIt13th

function isItThirtyONED(n) {

if (n === 7) {

return n;

}

if (n % 31 !== 0) {

console.log(n);

return n;

}

} //end of isItThirtyONED

function sumPrimes(num) {

var primes = [];

var i = 1;

while (i < num + 1) {

primes.push(i);

i++;

}

console.log("primes", primes);

primes = primes.filter(isItEven);

// Try 3. Take the number, and add the digits up, when those digits are divisible by 3, the number is not prime.

// Take 2469, those digits add up to 21, and 21 is divisible by 3, therefore 2469 is not a prime number.

primes = primes.filter(isItTHREED);

console.log("isItTHREED", primes);

// Try fours.

primes = primes.filter(isItFOURED);

console.log("isItFOURED", primes);

// Try fives

primes = primes.filter(isItFIVED);

console.log(primes);

// Try 7

primes = primes.filter(isItSeveneD);

console.log(primes);

// Try 11

primes = primes.filter(isItElevened);

console.log(primes);

// Try 13

primes = primes.filter(isIt13th);

console.log(primes);

// Try 31

primes = primes.filter(isItThirtyONED);

console.log(primes);

//might extract the anounoum function.. may also use it to filter out evens?

var reducted = primes.reduce((total, amount) => total + amount);

num = Number(reducted);

//console.log(num);

return num;

} //sumPrimes

sumPrimes(10); // should return 17.

//sumPrimes(100); // should return 17.

//sumPrimes(977); // should return 73156.

//2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 4146w - sorry, I may have been wrong.. or at least misread your intent.

233 lines of code was far less efficient than I was originally aiming for, but I learned quite a bit along the way. I guess that was the point.46w - Mr. Snow has a good youTube channel teaching this to High Schoolers, if it fits with your curriculum. freecodecamp.org - Learn to code and help nonprofits46w

Add a comment...