Functions/Methods within an Object Factory

Hello all!

I am running into an issue with my functions printing the results rather than just their name. I know the functions work (as far as I can tell at least) cause I can call for the functions individually and get the results needed but calling the whole factory gives me this output instead;

image

here is my code;

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

// Returns a random single strand 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 () {
      const randIndex = Math.floor(Math.random() * this.dna.length);
      let newBase = returnRandBase();
        while (this.dna[randIndex] === newBase) {
        return newBase;
      };
      this.dna[randIndex] = newBase;
      return this.dna;
    },
    compareDNA (otherPAqeour) {
      let count = 0;
      for (let i = 0; i < this.dna.length; i++) {
        if (this.dna[i] === otherPAqeour.dna[i]) {
          count += 1;
        }
      }
      console.log(`Specimen #${this.specimenNum} and Specimen #${otherPAqeour.specimenNum} have ${count * 100/this.dna.length}% DNA in common`);
    },
    willLikelySurvive () {
      let likleySur = this.dna.filter(el => el === "C" || el === "G");
      return likleySur.length / this.dna.length >= 0.6;
    }
  }
  }

let specimenSurvive = [];
let specimenCounter = 1;

while (specimenSurvive.length <= 30) {
  let newOrg = pAequorFactory(specimenCounter, mockUpStrand());
  if (newOrg.willLikelySurvive()) {
    specimenSurvive.push(newOrg);
  }
  specimenCounter++;
}

let ex1 = pAequorFactory(1, mockUpStrand());
let ex2 = pAequorFactory(2, mockUpStrand());


//console.log(ex1)
//console.log(ex1.mutate())
//console.log(ex1.compareDNA(ex2))
//console.log(ex1.willLikelySurvive())
console.log(specimenSurvive)

Any input would be greatly appreciated, I haven’t been able to find much help through articles and such.
I did also compare to the code results for the assignment and its giving the same kind of output. Makes me feel like I am missing something basic here.

thank you all in advance! :grin:

It will be helpful to have a link to this exercise so we can examine the instructions. Please post a link in a reply.

here you go!
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-22-javascript-syntax-part-ii/modules/wdcp-22-mysterious-organism/projects/mysterious-organism

1 Like

That loop does not permit reloading the random base.

let newBase;
while (...) {
  newBase = returnRandBase()
}
this.dna[...] = newBase
return this.dna

That’s one problem we can see. There may be others. Fix that and try again.

Fixed and sadly still getting the same output.

Okay, please post your new code and we’ll continue to examine it.

If you meant to print only the ids of the surviving specimens, you could do that with a loop.

for (let specimen of specimenSurvive) {
  console.log(specimen.id);
}

Or you could use .map for arrays to an array of the ids.

This method is logging instead of returning. Is that a possible issue?

Given these variables and method calls, the log out should appear as will follow…


const sp1 = mockUpStrand()
const sp2 = mockUpStrand()
const pq1 = pAequorFactory(1, sp1)
const pq2 = pAequorFactory(2, sp2)
console.log(pq1.dna);
console.log(pq1.willLikelySurvive());
console.log(pq2.dna);
console.log(pq2.willLikelySurvive());
console.log(pq1.compareDNA(pq2))
console.log(b = batch(1))
console.log(pAequorFactory(1, b[0]).complementStrand())

Which produces seven output streams:

[ 'A', 'C', 'G', 'T', 'T', 'T', 'C', 'C', 'G', 'A', 'G', 'A', 'C', 'G', 'C' ]
This specimen will likely survive
[ 'A', 'A', 'A', 'T', 'A', 'C', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'C', 'C' ]
This specimen will not likely survive
Specimen #1 and Specimen #2 have 40% DNA in common
[ [ 'T', 'A', 'C', 'T', 'G', 'C', 'C', 'A', 'G', 'T', 'T', 'C', 'G', 'G', 'T' ] ]
[ 'A', 'T', 'G', 'A', 'C', 'G', 'G', 'T', 'C', 'A', 'A', 'G', 'C', 'C', 'A' ]

This code is working the way I need it to. its pulling thirty specimens that survive from the random ones the factory spits out. Sorry I’m trying to get better about leaving comments in the code :slight_smile: Thank you!

So i just switched it and the when running the factory I still get just the function name printed it. If I print out the function directly it still prints correctly as well. Looks like it didn’t make a difference there :confused:

Calling each individual function does work, but i was trying to get the results from just calling survivingSpecimen arr. I don’t think its possible though. I was re-looking at the answer code they give and running theirs gives the same output of just the names;

At least I know I match the answer :sweat_smile:

edit: new code!

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

// Returns a random single strand 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 () {
const randIndex = Math.floor(Math.random() * this.dna.length);
let newBase;
while (this.dna[randIndex] === newBase) {
newBase = returnRandBase();
};
this.dna[randIndex] = newBase;
return this.dna;
},
compareDNA (otherPAqeour) {
let count = 0;
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === otherPAqeour.dna[i]) {
count += 1;
}
}
returnSpecimen #${this.specimenNum} and Specimen #${otherPAqeour.specimenNum} have ${count * 100/this.dna.length}% DNA in common;
},
willLikelySurvive () {
let likleySur = this.dna.filter(el => el === “C” || el === “G”);
return likleySur.length / this.dna.length >= 0.6;
}
}
}

let specimenSurvive = ;
let specimenCounter = 1;

while (specimenSurvive.length <= 30) {
let newOrg = pAequorFactory(specimenCounter, mockUpStrand());
if (newOrg.willLikelySurvive()) {
specimenSurvive.push(newOrg);
}
specimenCounter++;
}

let ex1 = pAequorFactory(1, mockUpStrand());
let ex2 = pAequorFactory(2, mockUpStrand());

//console.log(ex1)
//console.log(ex1.mutate())
//console.log(ex1.compareDNA(ex2))
//console.log(ex1.willLikelySurvive())
//console.log(specimenSurvive)`

This is the code I wrote many moons ago that gives the output above:

JS
const floor = Math.floor, random = Math.random;
const returnRandBase = () => 'ATCG'[floor(random() * 4)];
const mockUpStrand = function () {
  const newStrand = []
  Array(15).fill(null).forEach(function (_) {
    this.push(returnRandBase());
  }, newStrand);
  return newStrand;
}
const pAequorFactory = function (specimenNum, dna) {
  return {
    specimenNum,
    dna,
    mutate () {
      const x = floor(random() * 16)
      const y = this.dna[x]
      let z;
      do {
        z = returnRandBase()
      } while (y === z)
      this.dna[x] = z
      return this.dna
    },
    compareDNA (that) {
      return `Specimen #1 and Specimen #2 have ${
        round(
          this.dna.filter((x, i) => x === that.dna[i]).length / 15 * 10000
        ) / 100
      }% DNA in common`      
    },
    willLikelySurvive () {
      return `This specimen will${! (this.dna.filter(
        x => `CG`.includes(x)
      ).length / this.dna.length < 0.6) ? `` : ` not`} likely survive`
    },
    complementStrand() {
      const comp = {
        A: 'T',
        C: 'G',
        G: 'C',
        T: 'A'
      }
      const c = this.dna.slice()
      this.dna.forEach(function (x, i) {
        c[i] = comp[x]
      })
      return c
    }
  }
}
const batch = function (instances=30) {
  const pq=[]
  let i = 1
  while (pq.length < instances) {
    const mu = mockUpStrand()
    const nu = pAequorFactory(i, mu)
    if (nu.willLikelySurvive()) {
      pq.push(mu);
      i++;
    }
  }
  return pq
}
const sp1 = mockUpStrand()
const sp2 = mockUpStrand()
const pq1 = pAequorFactory(1, sp1)
const pq2 = pAequorFactory(2, sp2)
console.log(pq1.dna);
console.log(pq1.willLikelySurvive());
console.log(pq2.dna);
console.log(pq2.willLikelySurvive());
console.log(pq1.compareDNA(pq2))
console.log(b = batch(1))
console.log(pAequorFactory(1, b[0]).complementStrand())

It’s a bit different from yours, but near enough for comparison. Study how we arrive at the outputs in those seven streams.