Why doesn't this do... while loop work in my Mysterious Organism challenge project?

The task:

With the factory function set up, your team requests that you create 30 instances of pAequor that can survive in their natural environment. Store these instances in an array for your team to study later.

My code, with the part in question at the bottom:

// 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 = (specimenNum, dna) => {
	return {
		specimenNum,
		dna,
		mutate() {
			indexToChange = Math.floor(Math.random() * 15);
			baseToChange = this.dna[indexToChange];
		    while (this.dna[indexToChange] == baseToChange) {
		    	newBase = returnRandBase();
		    	this.dna[indexToChange] = newBase;
		    }
		    return this.dna;
		},
		compareDNA(pAequor) {
			sameBases = 0;
			for (let i = 0; i < this.dna.length; i++) {
				if (this.dna[i] == pAequor.dna[i]) sameBases += 1;
			}
			percentDiff = sameBases/15 * 100;
			return `Specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${percentDiff.toFixed()}% DNA in common.`;
		},
  willLikelySurvive() {
			let numCorG = 0;
			for (i of this.dna) {
				if (i == 'C' || i == 'G') numCorG += 1;
			}
			const percentCorG = numCorG/15 * 100;
			if (percentCorG >= 60) return true;
			return false;
		}
	};
}

let specimens = [];
for (let i = 0; i < 30; i++) {
	do specimens[i] = pAequorFactory(i, mockUpStrand());
	while (specimens[i].willLikelySurvive === false);
}

For comparison, I tested the following code, which DOES work with respect to the task of generating an array of only numbers between 0.0 and 0.5:

let test = [];
for (let i = 0; i < 5; i++) {
  do test[i] = Math.random();
  while (test[i] > 0.5);
}

But I don’t understand why this works and the project code does not.

Oh, I finally figured it out. I wasn’t including the parentheses to call the method.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.