Skip to main content
edited body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

trying to accumulate Accumulate the charAt(*) for each n

thisThis is a helper function, along with three other's itothers. It should be called as a .filter(); to return only prime numbers. It is for one of the exercises at FreeCodeCamp.com, Sum"Sum all PrimesPrimes".

I'm just trying to accumulate the charAt(*) for each n, so 10 would return 1, being 1+0 21 would return 3, being 2+1 and 977 would return 23, being 9+7+7.

I've commented out some of my attempts before thinking of charAt(0) and some n's will have as many as 3 digits, maybe more if I don't have to limit it, but the largest test case stops at 997 I think, but an extra decimal for margin of error. I finally got something to work, but it feels like I'm going the long way to get there.. Surely there's something already built into to java-scriptJavaScript I'm overlooking to reduce the value of a string. Or should I be trying to turn it into an array of digits?

function isItTHREED(n) {
  //  https://www.thoughtco.com/how-to-determine-number-is-prime-2312518

   //  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);
  //if(testing % 3 != 0)
    
  /*
  n = n.toString();
  n = n.split('');
  //num = num.split('');
  n = [n];
  console.log("num", n);
  */
   n = testing;
  return n;
} //end of isItTHREED

isItTHREED(10); // should return 1
isItTHREED(21); // should return 3
isItTHREED(977); // should return 23.

CodePen, works.. just feels bulky

trying to accumulate the charAt(*) for each n

this is a helper function, along with three other's it should be called as a .filter(); to return only prime numbers. It is for one of the exercises at FreeCodeCamp.com, Sum all Primes.

I'm just trying to accumulate the charAt(*) for each n, so 10 would return 1, being 1+0 21 would return 3, being 2+1 and 977 would return 23, being 9+7+7

I've commented out some of my attempts before thinking of charAt(0) and some n's will have as many as 3 digits, maybe more if I don't have to limit it, but the largest test case stops at 997 I think, but an extra decimal for margin of error. I finally got something to work, but it feels like I'm going the long way to get there.. Surely there's something already built into to java-script I'm overlooking to reduce the value of a string. Or should I be trying to turn it into an array of digits?

function isItTHREED(n) {
  //  https://www.thoughtco.com/how-to-determine-number-is-prime-2312518

   //  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);
  //if(testing % 3 != 0)
    
  /*
  n = n.toString();
  n = n.split('');
  //num = num.split('');
  n = [n];
  console.log("num", n);
  */
   n = testing;
  return n;
} //end of isItTHREED

isItTHREED(10); // should return 1
isItTHREED(21); // should return 3
isItTHREED(977); // should return 23.

CodePen, works.. just feels bulky

Accumulate the charAt(*) for each n

This is a helper function, along with three others. It should be called as a .filter(); to return only prime numbers. It is for one of the exercises at FreeCodeCamp.com, "Sum all Primes".

I'm just trying to accumulate the charAt(*) for each n, so 10 would return 1, being 1+0 21 would return 3, being 2+1 and 977 would return 23, being 9+7+7.

I've commented out some of my attempts before thinking of charAt(0) and some n's will have as many as 3 digits, maybe more if I don't have to limit it, but the largest test case stops at 997 I think, but an extra decimal for margin of error. I finally got something to work, but it feels like I'm going the long way to get there.. Surely there's something already built into to JavaScript I'm overlooking to reduce the value of a string. Or should I be trying to turn it into an array of digits?

function isItTHREED(n) {
  //  https://www.thoughtco.com/how-to-determine-number-is-prime-2312518

   //  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);
  //if(testing % 3 != 0)
    
  /*
  n = n.toString();
  n = n.split('');
  //num = num.split('');
  n = [n];
  console.log("num", n);
  */
   n = testing;
  return n;
} //end of isItTHREED

isItTHREED(10); // should return 1
isItTHREED(21); // should return 3
isItTHREED(977); // should return 23.

CodePen

Source Link

trying to accumulate the charAt(*) for each n

this is a helper function, along with three other's it should be called as a .filter(); to return only prime numbers. It is for one of the exercises at FreeCodeCamp.com, Sum all Primes.

I'm just trying to accumulate the charAt(*) for each n, so 10 would return 1, being 1+0 21 would return 3, being 2+1 and 977 would return 23, being 9+7+7

I've commented out some of my attempts before thinking of charAt(0) and some n's will have as many as 3 digits, maybe more if I don't have to limit it, but the largest test case stops at 997 I think, but an extra decimal for margin of error. I finally got something to work, but it feels like I'm going the long way to get there.. Surely there's something already built into to java-script I'm overlooking to reduce the value of a string. Or should I be trying to turn it into an array of digits?

function isItTHREED(n) {
  //  https://www.thoughtco.com/how-to-determine-number-is-prime-2312518

   //  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);
  //if(testing % 3 != 0)
    
  /*
  n = n.toString();
  n = n.split('');
  //num = num.split('');
  n = [n];
  console.log("num", n);
  */
   n = testing;
  return n;
} //end of isItTHREED

isItTHREED(10); // should return 1
isItTHREED(21); // should return 3
isItTHREED(977); // should return 23.

CodePen, works.. just feels bulky