JavaScript Challenge - Semi-Prime Numbers

This community-built FAQ covers the “Semi-Prime Numbers” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the JavaScript challenge Semi-Prime Numbers

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Solution to the problem-

function checkPrime(n){
  for (let i=2; i<=n/2; i++){
    if(n%i==0){
      return false;
    }
  }
  return true;
}
function checkSemiPrime(number) {
  let divisors=[];
  for (let i = 2; i * i <= number; ++i){
      if (number % i == 0) {
          divisors.push(i);
          if (i != number / i) divisors.push(number / i);
      }
  }
  let isPrime=true;
  if(divisors.length){
    for (let j = 0; j < divisors.length; ++j){
        if(!checkPrime(divisors[j])){
          isPrime=false
          break;
        }
    }
  }
  if(isPrime && divisors.length){
      return true;
    }
  return false;
}
function semiPrimeCount(n) {
  let counter=0;
  for (let i = 4; i < n; ++i){
    if(checkSemiPrime(i)){
      counter++;
    }
  }
  console.log('count',counter)
  return counter;
}
semiPrimeCount(10);
// Leave this so we can test your code:
module.exports = semiPrimeCount;

1 Like

function semiPrimeCount(n) {
let res=;
let doublon=true;
let z=null;
for (let x=2;x<n;x++) {
if(primenumber(x)){
for (let y=2;y<n;y++) {
if(primenumber(y)) {
z=x*y;
doublon=res.some(a => a==z);
if(doublon==false && z < n) res.push(z);
}
}
}
}
return res.length;
}

function primenumber(x){
if(x < 2) return false;
for (let i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0) return false;
}
return true;
}

semiPrimeCount(10);

// Leave this so we can test your code:
module.exports = semiPrimeCount;

function semiPrimeCount(n) {
  // pArray represents possible prime numbers
  const pArray = [];
    //Get prime Numers and put in pArray
  for (let i=2; i<n; i++) {
    //number to be divided by
    let isPrime = true;
    for (let j=Math.ceil(i/2); j>1; j--) {
      if (i === j) {
        continue;
      } else if (i%j === 0) {
        isPrime = false;
        break;
      }
    }
      if (isPrime) {
        if (!pArray.includes(i)) {
          pArray.push(i);
      }
    }
   }  
  // Calculate and count semiPrimes
  let count = 0;
  let semiPrimes = [];
  for (let x= 4; x<n; x++) {
    //y is index of pArray
    for (let y = 0; y < pArray.length; y++) {
      let semiPrime = false;
      if (!semiPrime) {
        // z is other index of pArray to multiply by y
        for (let z = 0; z<pArray.length; z++) {
    
          if ((pArray[z] * pArray[y] === x) && !semiPrimes.includes(x)) {
            semiPrimes.push(x)
            count++;
            semiPrime = true;
            break;
          }
        }
      }
    }
  }
  console.log(count);
  return count;
}

semiPrimeCount(10);

// Leave this so we can test your code:
module.exports = semiPrimeCount;