Mysterious Organism Challenge Project (JavaScript)

Hello everyone, I’m currently working on this project and am getting hung up on a strange issue. I am trying to randomly pick a base to mutate within the dna array but when I try accessing an element the console comes up with this error:

let mutatedBase = dna[10];
^

TypeError: Cannot read property ‘10’ of undefined
at Object.mutate (/home/ccuser/workspace/mystery-organism/main.js:24:28)

I’m not really sure what the problem is. Could someone 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
}

let dnaStrand = mockUpStrand();

const pAequorFactory = (specimenNum,dna) => {
  return {
    specimenNum: specimenNum,
    dna: dna,
    mutate(dna){
      var randomIndex = Math.floor(Math.random() * 15);
      let mutatedBase = dna[randomIndex];
      return randomIndex
      

    },
  };
};

//console.log(returnRandBase())

console.log(pAequorFactory(1,dnaStrand).mutate())





**the only code that I copied from the solution was the mutate() method. Everything else is my own original work.

Hi Codecademy,
My learning environment does not include the three pronged icon at the bottom of the page/screen
I have SAVE, the Copy to Clipboard icon, and the Reset icon only

Jeanine

(Javascript - Mysterious Organism(DNA) syntax Project · GitHub)

Hey
This is my Mysterious Organism solution.
It seems to work alright, but I was wondering why the output includes the factory function method objects?

I found this exercise obnoxious. Feel like the author was very lazy with an project that needed a LOT more steps broken down. Took 9 steps, crammed all instructions into 6 of them, and compacted 6 steps with about 40 steps of instructions. Who learns this way? I get it if you did like bullet points to demonstrate what is expected, but this really felt lazy and rushed. There is WAY too much going on here to leave up to interpretation. No outline, no demo, no nothing, just a few steps and here you go. I try to refrain from saying negative things here on this site but this wasn’t enjoyable. This wasn’t a learning experience. This was straight up annoying.

const pAequorFactory = (specimanNum, dna) => {
    return {
        specimanNum,
        dna,
        mutate() {
            const randIndex = Math.floor(Math.random() * this.dna.length);
            let newBase = returnRandBase();
            while (this.dna[randIndex] === newBase) {
                newBase = returnRandBase();
            }
            this.dna[randIndex] = newBase;
            return this.dna;
        },
        compareDNA(otherOrg) {
            const similarities = this.dna.reduce((acc, curr, idx, arr) => {
                if (arr[idx] === otherOrg.dna[idx]) {
                    return acc + 1;
                } else {
                    return acc;
                }
            }, 0);
            const percentOfDNAshared = (similarities / this.dna.length) * 100;
            const percentageTo2Deci = percentOfDNAshared.toFixed(2);
            console.log(`${this.specimanNum} and ${otherOrg.specimanNum} have ${percentageTo2Deci}% DNA in common.`);
        },
        willLikelySurvive() {
            const cOrG = this.dna.filter(el => el === "C" || el === "G");
            return cOrG.length / this.dna.length >= 0.6;
        },
    }
};

Asking anyone to write that much up and put different functionality is bad form and practice. Functions don’t belong there and makes it hard to read. You would use code analyzers in real life and Sonar rates this as 15 as in Are you Kidding? as in is this some kind of joke? Why? The complexity is beyond ridiculous.


Apologize for the rant, but I do pay for this site and feel like I can at least leave my opinion on the absurdity here.

Your mutate function is expecting a parameter :slight_smile:

Hiya,

I’ve just finished this task.

That’s how mine displays if I log the generated array. Is that not how the objects should be?

Thanks

Heyo,

Just finished this one.

GitHub

A fair bit is different to the Codecademy solution but I’m pretty sure it all works.

Made the function that builds the array of samples that survive take a number argument so you can re-use with different input amounts.

Thanks in advance for any advice.

Here is my solution! I feel like this project was where I broke through some of the difficulties I was having. Good luck to those currently working on it!

My solution for part 4:

// Factory function
const pAequorFactory = (specimenNum, dna) => {
  return {
    specimenNum: specimenNum,
    dna: dna,
    mutate() {
      const randomNum = Math.floor(Math.random() * this.dna.length) // Generate random num
      let newBase = returnRandBase() // Generate new DNA strand
      while(this.dna[randomNum] === newBase) { // While DNA strands match...
        newBase = returnRandBase() //...try again until new DNA strand is found
    }
      return this.dna.splice(randomNum, 1, newBase) // Mutate DNA strand 
    } 
  }
};

Thank you! Realized I missed the .this keyword.

Hello,

Here is my code solution for the Mysterious Organism Challenge Project. I show multiple code approaches (solutions) for Items 5 and 6, and show code solutions for the Item 9 challenges. Best Regards, Bear . . .

My solution.

Hey guys and gals, I am up to number 4 on this project. Below is my code:

// Returns a random DNA base

const returnRandBase = () => {

const dnaBases = [‘A’, ‘T’, ‘C’, ‘G’]

return console.log(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 = (uniqueNum, dnaStrand) => {

return {

specimenNum: uniqueNum,

dna: dnaStrand,



mutate() {

console.log(' ')

let mutation = []



for(let i of this.dna) {

  mutation.push(returnRandBase(i));

}

return mutation

}

}

}

pAequorFactory(1, mockUpStrand()).mutate();

I am looking to modify my .mutate method, but I’m not entirely sure how to get it to only change a single specified character. I am wondering if this is a series of if-else if statements. It really is the only thing I can think of at the moment. Any feedback would be great. Thanks in advance!

Try accessing an the index of this.dna. Than you can mutate it. I did it with generating a random index and than a while-loop that changes the base at the random index until the base is different to the old one!

Hi there! This is my solution … any comment is welcome!

Thanks,
Dirk

Hello everyone!

This project took me way way longer that I am proud to admit but I’m really glad I stuck with it to the end.

I realized I was not really comfortable with the concept of comparing arrays which was made obvious when I got stuck in the compare() method and later, when looking for a way to find the most related DNA strands.

I’m also glad that I stuck to learning how to share my code with Github.
Here is my code:

Hey everyone, here is my solution.

Feel free to give me some advices and constructive critics. :slight_smile: