Okay, I’ve adjusted my code according to your email I believe (below) but the output is still “false” every time I test it, even after putting “1” in place of the “8”. I get false every time 20 times in a row. I’m not sure how to determine whether or not it’s functioning properly.
willLikelySurvive() {
count = 0;
for (let j of this.dna) {
if (j === ‘C’ || j === ‘G’) {
count++;
}
return count > 8
}
}
Okay so I tried that (below) but I keep getting “false” as the output still. I tried replacing the “test it” section with what you provided (console.log(count > 8)) but VSCode threw an error saying “count” wasn’t defined, even though it is. I also tried moving the console.log up to the “tried here” line but then it said the “.” in “console.log” was invalid. Do you have any insight?
willLikelySurvive() {
count = 0;
for (let x of strand) {
if (‘CG’.includes(x)) {
count++;
}
return count > 8
}
}
// tried here
// Factory function - takes a number and an array of DNA bases (15)
const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum,
dna,
// Mutates a random base
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;
// return mutated strand
return this.dna;
},
// test it!
// const strand = mockUpStrand()
// const mutatedStrand = pAequorFactory(1, strand)
// console.log(mutatedStrand.mutate());
// Compares dna, returns similarity as percentage
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
console.log(`Strand ${this.specimenNum} and strand ${pAequorObj.specimenNum} have ${Math.floor(inCommon)}% DNA in common.`);
},
// test it!
// strand1 = mockUpStrand()
// strand2 = mockUpStrand()
// pass each to the factory and assign the returns to two variables.
// specimen1 = pAequorFactory(1, strand1)
// specimen2 = pAequorFactory(2, strand2)
// call the compare method on one with the other as argument.
// specimen1.compareDNA(specimen2);
// Determines if 'C' and 'G' bases make up >=60% of dna
willLikelySurvive() {
count = 0;
for (let x of this.dna) {
if ('CG'.includes(x)) {
count++;
}
return count > 8
}
}
}
}
// test it!
strand = 'CAGCACTGCAGTACG'.split('')
survivalStrand = pAequorFactory(1, strand)
console.log(survivalStrand.willLikelySurvive());
Suggest remove all the comments (yes, all of them) and write the code in nested form (showing indents). Repost, but first be sure you know how to post code samples so readers can see the nesting.
Note the use of arrow function syntax in the first one. It’s simple, concise and consistent. No verbosity, one readable line. We use the logic in several cases so it is definitely a useful helper function. Even though the code is brief, and we could use just that, it means writing the same code again, later. Better it is abstracted to a standalone helper.
The second function is not a use case for an arrow function for three reasons: 1) It has internal variables declared; 2) it has braces and return; and, 3) there is no added benefit. Our callback also uses a standard function expression so there is a this property. Read up on the above usage of Array.forEach().
It’s an author choice, in this case since we can do the same thing with a simple for loop. More taking artistic license using a dummy array to expose the .forEach() method. The signature line is where it starts and ends. Even the parameter is _ which indicates not used. It just guarantees our newStrand array has 15 elements.
From here it’s on to build the factory function. What you have can use a couple more changes, but you’re getting so close to a finished project. Ping us when you are ready to review your three methods, top to bottom. One last trip through the process so you have working code and some take away from this project.