Mystery organism - member function doesn't return mutated array

Hi there!

I’m reaching out to see if you might be able to help me out with testing my code. I’m working on the Mystery Organism project and I’m trying to test the mutate method in my object, but it seems as though either my method isn’t working OR the way I’m testing it is incorrect. Are you able to point me in the right direction?

// 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() {
const randBase = Math.floor(Math.random() * this.dna.length);
let newBase = returnRandBase();
while (this.dna[randBase] === newBase) {
newBase = returnRandBase();
}
this.dna[randBase] = newBase;
//console.log(The mutated dna base has become: ${newBase});
//console.log(The mutated dna base has become: ${this.dna});
return this.dna;
}
}
}

let pAequor1 = pAequorFactory(1, ‘A’);
//let pAequor2 = pAequorFactory(2, ‘G’);

console.log(pAequor1.mutate());
//console.log(pAequor2.mutate());

Thanks!
Megan

We have 15 bases of which we wish to change only one, and be sure of this effect. That will require a loop in the function.

  • randomly select a base
  • get a random base
  • compare and repeat 1 and 2 if identical
  • mutate the strain and return

My Firefox browser crapped out and I had to reinstall, and lose all the bookmarks. Please post a link to this project so I can look at my old code. Thanks.

Hi Roy,

Thanks for breaking down the steps! However, I’m still unsure of how to test my code correctly. Any input you have on that would be much appreciated.

Here is the project in Codecademy:

https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-22-javascript-syntax-part-ii/modules/wdcp-22-mysterious-organism/projects/mysterious-organism

And here is the main forum for this project:

https://discuss.codecademy.com/t/mysterious-organism-challenge-project-javascript/462376/2

1 Like
const strand = mockUpStrand()
const pStrand = pAequorFactory(1, strand)

Now we can call on the mutate method…

console.log(pStrand.mutate())

Wow thank you so much! I had been stuck at that part for hoursss

1 Like

Hi Roy,

I’m back with another question about a difference section of the Mysterious Organisms project. I thought I implemented this portion of functionality correctly, but when I test it using the code at the bottom, the output is “[Function: compareDNA]”. Are you able to find where I’ve gone wrong? Below my code are the instructions from Codecademy.

Thank you!
Megan

compareDNA(pAequorObj) {
//compare current pAequor dna with passed in pAequor dna
let inCommon = 0;
for (let i=0; i < this.dna.length; i++) {
//compute how many bases are identicle in the same locations
if (this.dna[i] === pAequorObj.dna[i]) {
inCommon++
}
}
inCommon/=15
inCommon*=100
//print message that states % of dna the two obj have in common
//use .specimenNum to identify which pAequor objects are bring compared
console.log(${this.specimenNum} and ${pAequorObj.specimenNum} have ${inCommon}% DNA in common.);
}
}
}
const fakeStrand = mockUpStrand()
const testStrand = pAequorFactory(3, fakeStrand)
console.log(testStrand.compareDNA);

"Your research team wants to be able to compare the DNA sequences of different P. aequor. You’ll have to add a new method (.compareDNA()) to the returned object of the factory function.

.compareDNA() has one parameter, another pAequor object.

The behavior of .compareDNA() is to compare the current pAequor‘s .dna with the passed in pAequor‘s .dna and compute how many bases are identical and in the same locations. .compareDNA() does not return anything, but prints a message that states the percentage of DNA the two objects have in common — use the .specimenNum to identify which pAequor objects are being compared."

You need to invoke the method and pass in the other strand. Create two strands using the factory function. Invoke the method on one of them, and pass in the other one.

Hi Roy,

Thank you for this. I’m not sure what to do because I don’t know how to create two strands using the factory function. I’m also not sure how to invoke the method on one of them, and pass in the other one (I don’t understand the difference between invoke and pass in). I tried watching some factory function tutorials but I’m still unsure of how to use factory functions after building them. Any insight you have would be appreciated.

Thank you,

Megan

strand1 = mockupstrand()
strand2 = mockupstrand()

Now pass each to the factory and assign the returns to two variables. Next call the compare method on one with the other as argument.

1 Like

Okay! I think I did it! :slight_smile: Thank you for your patience and assistance!


//print message that states % of dna the two obj have in common
//use .specimenNum to identify which pAequor objects are bring compared
console.log(${this.specimenNum} and ${pAequorObj.specimenNum} have ${inCommon}% DNA in common.);
}
}
}
//create two strands
strand1 = mockUpStrand()
strand2 = mockUpStrand()

//Now pass each to the factory and assign the returns to two variables.
variable1 = pAequorFactory(1, strand1)
variable2 = pAequorFactory(2, strand2)

//Next call the compare method on one with the other as argument.
variable1.compareDNA(variable2);

Output:

1 and 2 have 20% DNA in common.

1 Like

Hi Roy,

I’ve moved onto the next step of the project and I’ve run into TypeErrorI’m unable to resolve. Are you able to provide any insight?

Thank you!

Megan

P. aequor have a likelier chance of survival if their DNA is made up of at least 60% 'C' or 'G' bases. In the returned object of pAequorFactory(), add another method .willLikelySurvive()``. ``.willLikelySurvive() returns true if the object’s .dna array contains at least 60% 'C' or 'G' bases. Otherwise, .willLikelySurvive() returns false.

willLikelySurvive(pAequorObj) {
const totalGs = ;
const totalCs = ;
for (let i=0; i < 15; i++) {
if (pAequorObj.includes(‘G’)) {
totalGs.push(i);
}
else if (pAequorObj.includes(‘C’)) {
totalCs.push(i);
}
}
if (totalGs.length >= 9 || totalCs.length >= 9) {
return true
} else {
return false
}
}
}
}

pAequorObj = mockUpStrand()
test1 = pAequorFactory(1, pAequorObj)
console.log(test1.willLikelySurvive());

output: if (pAequorObj.includes(‘G’)) {

^
TypeError: Cannot read property ‘includes’ of undefined
at Object.willLikelySurvive

60% of 15 is 9. Regardless whether ‘C’ or ‘G’ we only need to count them as 1. No arrays needed, just a counter.

count = 0
for (let x of this.dna) {
    if ('CG'.includes(x)) {
        count++
    }
}
return count > 8

Recall that we are using instances, not globals.

strand = mockUpStrand()
strain = new pAequorFactory(1, strand)
console.log(strain.willLikelySurvive())

Be sure your method takes no parameters.

Recall that the numeric equivalent of true is 1 and of false is 0.

count = 0
for (let x of this.dna) {
  count += +'CG'.includes(x)
}
return count

Above we use the unary + operator to coerce an integer. Sneaky, eh?

Hmm okay! I want to make sure I’m understanding correctly in both of the examples you provided!

When you say:

count = 0

for (let x of this.dna) {

    if ('CG'.includes(x)) {

        count++

    }

}

return count > 8

You are saying that we iterate through each element of this.dna and if an element includes ‘CG’ (which I assume would translate into saying “either C or G”), add one to the count, then return counts that are over 8. Is this correct? And when you say:

count = 0

for (let x of this.dna) {

  count += +'CG'.includes(x)

}

return count

You are saying that we iterate through each element of this.dna and our count will increase by one each time either C or G is detected in the array. Is this correct?

Thanks!
Megan

1 Like

Yes.

Actually, return a boolean true if the count is over 8. Relational operations are always boolean.

Oh okay! Thank you for your help! :slight_smile:

1 Like

Hi Roy,

I’m on the last step of this project and I have a question about the output. I wrote the following code and it produces the following output. I noticed that in the output below the new specimen I create, there is text that says:

"mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]"

Do you know why this text is here? Do you know how I might be able to make adjustments so that it’s not included in the output?

Thanks!
Megan

//create 30 instances of pAequor and store in an array
const specimenArray = ;
let i = 1;
let newSpecimen = pAequorFactory(i, mockUpStrand());
for (let i=1; i<=30; i++) {
if (newSpecimen.willLikelySurvive() === true) {
specimenArray.push(pAequorFactory(i, mockUpStrand()));
} else if (newSpecimen.willLikelySurvive() === false) {
specimenArray.pop();
}
}

console.log(specimenArray);

Output:

[
{
specimenNum: 1,
dna: [
‘A’, ‘A’, ‘C’, ‘G’,
‘A’, ‘C’, ‘T’, ‘A’,
‘G’, ‘C’, ‘C’, ‘A’,
‘A’, ‘G’, ‘G’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},
{
specimenNum: 2,
dna: [
‘G’, ‘T’, ‘C’, ‘A’,
‘G’, ‘G’, ‘A’, ‘A’,
‘T’, ‘C’, ‘C’, ‘C’,
‘A’, ‘C’, ‘A’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},

(and so on for 30 specimen)

That output usually indicates the methods have been called but not invoked. Be sure the calls have ().

Please post your entire code (formatted).

Hi Roy,

Here’s my code in its entirety! Also, I just noticed something super strange - when I went to delete an empty row in the mutate() method, or delete a comment that I had made earlier that just said “//test it” (which I didn’t think would affect my code?), the output completely changed from producing 30 specimens to an output that just said “”. I have no idea why, do you know?

Also I thought I’d successfully tested the compareDNA() method earlier, but looking back, the output was just from the console.log() line before it. I’m still unsure of how to test the compareDNA() method. When I tried, there was no output.

// 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() {
//selecting random base from this dna
const randBase = Math.floor(Math.random() * this.dna.length);

//establish random base as oldBase
let oldBase = this.dna[randBase];

//establish replacement base as newBase
let newBase = returnRandBase();

//compare oldBase to newBase until they don't match and mutate
do {
newBase = returnRandBase();
} while (newBase === oldBase);
oldBase = newBase;

//console.log(`The mutated dna base has become: ${newBase}`);
//return mutated strand
return this.dna;
},
//test it!
//const strand = mockUpStrand()
//const pStrand = pAequorFactory(1, strand)
//console.log(pStrand.mutate());

compareDNA(pAequorObj) {
//compare current pAequor dna with passed in pAequor dna
let inCommon = 0;
for (let i=0; i < 15; i++) {
//compute how many bases are identicle in the same locations
if (this.dna[i] === pAequorObj.dna[i]) {
inCommon++
}
}
inCommon/=15
inCommon*=100
//print message that states % of dna the two obj have in common
//use .specimenNum to identify which pAequor objects are bring compared
console.log(`Strand ${this.specimenNum} and strand ${pAequorObj.specimenNum} have ${Math.floor(inCommon)}% DNA in common.`);
},
//test it!
//strand1 = mockUpStrand()
//strand2 = mockUpStrand()

//Now pass each to the factory and assign the returns to two variables.
//variable1 = pAequorFactory(1, strand1)
//variable2 = pAequorFactory(2, strand2)

//Next call the compare method on one with the other as argument.
//variable1.compareDNA(variable2);

willLikelySurvive() {
count = 0;
for (let i of this.dna) {
if ('CG'.includes(i)) {
count++
}
}
return count > 8
}
}
}
//test it!
//strand = mockUpStrand()
//strain = pAequorFactory(1, strand)
//console.log(strain.willLikelySurvive());

//create 30 instances of pAequor and store in an array
const specimenArray = [];
let i = 1;
let newSpecimen = pAequorFactory(i, mockUpStrand());
for (let i=1; i<=30; i++) {
if (newSpecimen.willLikelySurvive() === true) {
specimenArray.push(pAequorFactory(i, mockUpStrand()));
} else if (newSpecimen.willLikelySurvive() === false) {
specimenArray.pop();
}
}

console.log(specimenArray);

Output:

[
{
specimenNum: 1,
dna: [
‘C’, ‘C’, ‘T’, ‘C’,
‘G’, ‘C’, ‘T’, ‘A’,
‘A’, ‘C’, ‘G’, ‘T’,
‘T’, ‘G’, ‘T’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},
{
specimenNum: 2,
dna: [
‘G’, ‘T’, ‘G’, ‘T’,
‘G’, ‘C’, ‘C’, ‘G’,
‘A’, ‘A’, ‘G’, ‘A’,
‘T’, ‘C’, ‘G’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},
{
specimenNum: 3,
dna: [
‘C’, ‘T’, ‘A’, ‘C’,
‘G’, ‘C’, ‘G’, ‘T’,
‘G’, ‘A’, ‘A’, ‘G’,
‘A’, ‘C’, ‘G’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},
{
specimenNum: 4,
dna: [
‘A’, ‘A’, ‘A’, ‘A’,
‘A’, ‘A’, ‘T’, ‘C’,
‘G’, ‘A’, ‘G’, ‘T’,
‘C’, ‘G’, ‘A’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},

etc etc etc

Thanks!
Megan