Mystery organism - member function doesn't return mutated array

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

// test it!
strand = mockUpStrand()
survivalStrand = pAequorFactory(1, strand)
console.log(survivalStrand.willLikelySurvive());

One could manually write a test strand that contains the requisite number of C and G…

strand = 'CAGCACTGCAGTACG'.split('')
count = 0
for (let x of strand) {
    if ('CG'.includes(x)) {
        count++;
    }
}
console.log(count > 8)
// true

Oh of course! :slight_smile:

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

}
}
// test it!
strand = ‘CAGCACTGCAGTACG’.split(’’)
survivalStrand = pAequorFactory(1, strand)
console.log(survivalStrand.willLikelySurvive());

// false

Be sure to use this.dna in the method. My example is freestanding code.

Okay I just ran the code below and I’m still getting false :frowning: Is the code in my “test it” section correct?

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());

Let’s have another look at your complete factory function, please.

Sure thing! Here it is:

// 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());

Hard to tell what is what. All the methods should be confined to the returned object.

The object should have two properties and three methods.

Invoke a new instance of the object,

instance = factory(num, strand)

Now check if the instance has all the methods…

instance.hasOwnProperty('compareDNA')
instance.hasOwnProperty('mutate')
instance.hasOwnProperty('willLikelySurvive')

Okay I put the following after my object code, but there was no output. Did I do it wrong?

strand = ‘CAGCACTGCAGTACG’.split(’’)
instance = pAequorFactory(3, strand)

instance.hasOwnProperty(‘compareDNA’)

instance.hasOwnProperty(‘mutate’)

instance.hasOwnProperty(‘willLikelySurvive’)

Sorry, should have said to log the last three lines. They should all log true.

Got it! They all logged true :slight_smile: Okay so I wonder my the willLikelySurvive still isn’t functioning properly?

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.

const pAequorFactory = (specimenNum, dna) => {
  return {
    specimenNum,
    dna,
    mutate() {
      const randBase = Math.floor(Math.random() * this.dna.length);
      let oldBase = this.dna[randBase];
      let newBase = returnRandBase();
      do {
        newBase = returnRandBase();
      } while (newBase === oldBase);
      oldBase = newBase;
      return this.dna;
    },

    compareDNA(pAequorObj) {
      let inCommon = 0;
      for (let i = 0; i < 15; i++) {
        if (this.dna[i] === pAequorObj.dna[i]) {
          inCommon++
        }
      }
      inCommon /= 15
      inCommon *= 100 
        console.log(`Strand ${this.specimenNum} and strand ${pAequorObj.specimenNum} have ${Math.floor(inCommon)}% DNA in common.`);
    },
          
    willLikelySurvive() {
      count = 0;
      for (let x of this.dna) {
        if ('CG'.includes(x)) {
          count++;
        }
        return count > 8
      }
    }
  }
};
1 Like

That line is insider the loop. It should be after the loop.


Sharing my helper functions…

// use shorter Math method names
const floor = Math.floor, random = Math.random, round = Math.round;

const returnRandBase = () => 'ATCG'[floor(random() * 4)];
// console.log(returnRandBase());

const mockUpStrand = function () {
  const newStrand = [];
  Array(15).fill(null).forEach(function (_) {
    this.push(returnRandBase());
  }, newStrand);
  return newStrand;
}
// console.log(mockUpStrand());

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.

Ohh okay! I thought I understood how loops worked but apparently I was wrong! I see now how that was my error. Thank you for your help!

1 Like

Hello I’m 32% through the Frond End Developer path. This is my solution for the mysterious organism challenge Mysterious-Creature/main.js at main · fquinn454/Mysterious-Creature · GitHub
All feedback gratefully received.
Thanks,
Fiona