Mysterious Organism

I am working on Mystery Organism and i am stuck with #7. we are making a factory function that creates an object with one of the properties is named specimenNum. i need specimenNum to increase by one for each object created and im having trouble doing that. This is my code so far.

// 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, seq) => {
  return {
    specimenNum: num++,
    dna: seq,
    mutate(){
      let idx = Math.floor(Math.random() * 15);
      let x = this.dna[idx];
      let y = '';
      do {
        y = returnRandBase();
      } while (x === y);
      if (x !== y) {
        this.dna.splice(idx, 1, y);
      }
      return this.dna;
    },
    compareDNA(obj) {
      let match = 0
      for (x = 0; x < this.dna.length; x++) {
        if (this.dna[x] === obj[x]) {
          match++;
        }
      }
      let percenti = match/obj.length * 100
      let percent = (match/obj.length * 100).toFixed(2);
      return `The two DNA strands have ${percent}% DNA in common.`
    },
    willLikelySurvive(){
      let match = 0
      for (x = 0; x < this.dna.length; x++) {
        if (this.dna[x] === 'C' || this.dna[x] === 'G') {
          match++;
        } 
      }
      let percent = match/this.dna.length * 100;
      if (percent < 60) {
        return false;
      }
      return true;
    }
  }
};


let arr = [];
let count = 0
let match = 0;
do {
  let test10 = pAequorFactory(1, mockUpStrand());
  test10 = test10.willLikelySurvive();
  console.log(test10);
  count++;
  if (test10 === true) {
    match++;
  }
} while (match < 30);
console.log(match)
console.log(count)

never mind i figured it out,
my finished code is:

// 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, seq) => {
  return {
    specimenNum: num,
    DNA: seq,
    mutate(){
      let idx = Math.floor(Math.random() * 15);
      let x = this.DNA[idx];
      let y = '';
      do {
        y = returnRandBase();
      } while (x === y);
      if (x !== y) {
        this.DNA.splice(idx, 1, y);
      }
      return this.DNA;
    },
    compareDNA(obj) {
      let match = 0
      for (x = 0; x < this.DNA.length; x++) {
        if (this.dna[x] === obj[x]) {
          match++;
        }
      }
      let percenti = match/obj.length * 100
      let percent = (match/obj.length * 100).toFixed(2);
      return `The two DNA strands have ${percent}% DNA in common.`
    },
    willLikelySurvive(){
      let match = 0
      for (x = 0; x < this.DNA.length; x++) {
        if (this.DNA[x] === 'C' || this.DNA[x] === 'G') {
          match++;
        } 
      }
      let percent = match/this.DNA.length * 100;
      if (percent < 60) {
        return false;
      }
      return true;
    }
  }
};


let arr = [];
let count = 0
let match = 0;
let counter = 1
let counters = 0
do {
  let test10 = pAequorFactory(counter, mockUpStrand());
  test11 = test10.willLikelySurvive();
  counter++
  if (test11) {
    match++;
    counters++;
    arr.push({Test: test10.specimenNum, Completed: counters, DNA: test10.DNA})
  }
} while (match < 30);
console.log(arr);
4 Likes

thanks for posting your code it really helped me understand what to do in the project. I tried to only look at it for reference rather than a cheat.

4 Likes

Bravo! :clap: :clap: :clap:

I’m currently stuck on this milestone/task

4)Your team wants you to simulate P. aequor ‘s high rate of mutation (change in its DNA).

To simulate a mutation, in pAequorFactory() ‘s returned object, add the method .mutate() .

.mutate() is responsible for randomly selecting a base in the object’s dna property and changing the current base to a different base. Then .mutate() will return the object’s dna.

What is the current base that the task refers to here? Is it dna[specimenNum]?

Hi!

I’m testing your code and the the method .compareDNA returns “The two DNA strands have NaN% DNA in common”. As far as I know NaN stands for “Not a number”. Do you have any idea what goes wrong here?

This is my piece of code steps 1-5. It looks exactly like yours - I just changed some names and added console.log lines to test the code.

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

//console.log(returnRandBase());

// 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
}

//console.log(mockUpStrand());

//3-4
const pAequorFactory = (number, array) => {
  return {
    specimenNum: number,
    DNA: array,

    mutate() {
      let index = Math.floor(Math.random() * 15);
      let x = this.DNA[index];
      let y = '';
      do {
        y = returnRandBase();
      } while (x === y);
      if (x !== y) {
        this.DNA.splice(index, 1, y);
      }
      return this.DNA;
     },

//5
    compareDNA(obj) {
      let match = 0
      for (i = 0; i < this.DNA.length; i++) {
        if (this.DNA[i] === obj[i]) {
          match++;
        }
      }
      let percenti = match/obj.length * 100
      let percent = (match/obj.length * 100).toFixed(2);
      return `The two DNA strands have ${percent}% DNA in common.`
    }
   }
  };

let species1 = pAequorFactory(1, mockUpStrand());
console.log(species1.DNA)
species1.mutate();

let species2 = pAequorFactory(2, mockUpStrand());
console.log(species2.DNA)
species2.mutate();

console.log(species1.compareDNA(species2));

The output:

[ ‘A’, ‘A’, ‘C’, ‘A’, ‘G’, ‘A’, ‘T’, ‘C’, ‘G’, ‘A’, ‘C’, ‘C’, ‘G’, ‘A’, ‘G’ ]
[ ‘G’, ‘T’, ‘C’, ‘T’, ‘G’, ‘C’, ‘A’, ‘C’, ‘G’, ‘T’, ‘C’, ‘C’, ‘G’, ‘G’, ‘T’ ]
The two DNA strands have NaN% DNA in common.

PS. I’m having red brackets, I guess there’s an error somewhere but have no idea where - I went through the entire code many times but still no clue.

It’s because you’re trying to compare against and find the length of the Object whereas it should be the DNA property of the object.

How would you write that line of code?

obj.DNA[i]

So:

compareDNA(obj) {
      let match = 0
      for (i = 0; i < this.DNA.length; i++) {
        if (this.DNA[i] === obj.DNA[i]) {
          match++;
        }
      }
      let percenti = match/obj.length * 100 //this isn't even being used
      let percent = (match/obj.DNA.length * 100).toFixed(2);
      return `The two DNA strands have ${percent}% DNA in common.`
    }
   }
  };
1 Like

Ahh right! Thank you very much :slight_smile: