Need help with mysterious organism step 7

// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = [‘A’, ‘T’, ‘C’, ‘G’]
return dnaBases[Math.floor(Math.random() * 4)]
}

// Returns a random single stand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand =
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}

const pAequorFactory = (num, arr) => {
return {
specimenNum: num,
dna: arr,
mutate(){
let random = Math.floor(Math.random() * 15);
let randomBase = this.dna[random];
let randStand = returnRandBase();
for (let r = 0; r < randStand.length; r++){
if (randomBase[random] !== randStand[r]){
this.dna[random] = randStand[r];
} else {
false;
}
return this.dna
}
},
compareDNA(otherDna) {
let count = 0;
for (let i = 0; i < this.dna.length; i++){
if (this.dna[i] === otherDna.dna[i]){
count++
}
}
let percent = (count / this.dna.length * 100).toFixed();

    return `${this.specimenNum} and ${otherDna.specimenNum} have ${percent}% DNA in commom.`; 
},
willLikelySurvive(){
  let base = 0;
  for (let b = 0; b <= this.dna.length; b++){
    if (this.dna[b] === 'C' || this.dna[b] === 'G'){
      base++;
    }
  }
  let chance = ((base / this.dna.length) * 100).toFixed();
  if (chance > 59) {
    return true;
  } else {
    return false;
  }
},
complementStrand(){
  let complementDNA = this.dna.map(element => {
    switch (element) {
      case 'A' : return 'T';
      break;
      case 'T' : return 'A';
      break;
      case 'G': return 'C';
      break;
      case 'C': return 'G';
      break;
    }
  })
  return complementDNA;
}

};
}

const tests = () => {
let survivors = ;
let x = 1;
while (survivors.length < 30) {
let test = pAequorFactory(x, mockUpStrand());
x++;
if (test.willLikelySurvive === true){
survivors.push(test);
}
} return survivors;
}

What am I doing wrong on getting the 30 instances?!?

Could you format your post, please? That makes it easier for everyone to read it and help.
https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351

I also think it’s better if you format your code so that it’s easier to read.

sorry and thanks for the link

// Returns a random DNA base
const returnRandBase = () => {
  const dnaBases = ['A', 'T', 'C', 'G']
  return dnaBases[Math.floor(Math.random() * 4)] 
}

// Returns a random single stand of DNA containing 15 bases
const mockUpStrand = () => {
  const newStrand = []
  for (let i = 0; i < 15; i++) {
    newStrand.push(returnRandBase())
  }
  return newStrand
}

const pAequorFactory = (num, arr) => {
  return {
    specimenNum: num,
    dna: arr,
    mutate(){
      let random = Math.floor(Math.random() * 15);
      let randomBase = this.dna[random];
      let randStand = returnRandBase();
      for (let r = 0; r < randStand.length; r++){
      if (randomBase[random] !== randStand[r]){
        this.dna[random] = randStand[r];
      } else {
        false;
      }
      return this.dna
     }
    },
    compareDNA(otherDna) {
      let count = 0;
      for (let i = 0; i < this.dna.length; i++){
        if (this.dna[i] === otherDna.dna[i]){
          count++
         }
        }
        let percent = (count / 15  * 100).toFixed();
  
        return `${this.specimenNum} and ${otherDna.specimenNum} have ${percent}% DNA in commom.`; 
    },
    willLikelySurvive(){
      let base = 0;
      for (let b = 0; b <= this.dna.length; b++){
        if (this.dna[b] === 'C' || this.dna[b] === 'G'){
          base++;
        }
      }
      let chance = ((base / this.dna.length) * 100).toFixed();
      if (chance > 59) {
        return true;
      } else {
        return false;
      }
    },
    complementStrand(){
      let complementDNA = this.dna.map(element => {
        switch (element) {
          case 'A' : return 'T';
          break;
          case 'T' : return 'A';
          break;
          case 'G': return 'C';
          break;
          case 'C': return 'G';
          break;
        }
      })
      return complementDNA;
    }
  };
}


const tests = () => {
  let survivors = [];
  let x = 1;
  while (survivors.length < 30) {
    let test = pAequorFactory(x, mockUpStrand());
    x++;
    if (test.willLikelySurvive === true){
      survivors.push(test);
    }
  } return survivors;
}

//const zero = pAequorFactory(0, mockUpStrand());
//console.log();
//console.log(zero.complementStrand())
//console.log(zero.dna)
//console.log(zero.mutate());
//console.log(zero.willLikelySurvive())







1 Like

Can you specify your problem, please? What do you mean by 30 instances (of what after doing what)?
On first glance, your factory function seems to do what it should.

Calling this just creates one instance of pAequor.

on this part. We have to create 30 instances that the organism will survive. So this it suppose to run the pAequorFactory a bunch of times till there are 30 paequor in the arr. However when I run it it dosen’t show any surviving. I checked my willLikelySurvive function and it works so it has to be the tests function.

You are running an infinite loop, because this

is never true.
If you console.log(test.willLikelySurvive) you’ll see that it prints the whole function, because you forgot the parenthesis.
You also push a function to the survivors array, because this pAequorFactory(x, mockUpStrand()) just stores the function.

1 Like