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())
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
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.
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.
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!
// 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
}
}
};
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 . . .
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!
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: