You mutatedBase
has problems which is it can not be duplicated with the remove target base. So in this case you must check if oldBase === mutatedBase
and we have to regenerate it
in the mutate()
property, why do you have to recreate new DNA’s base?
const selectDnaBase = Math.floor(Math.random() * 15);
you have to use its own DNA by using this.dna
and mutate only 1 base in 15 bases of its DNA
this is my code, you can check and we can discuss together!
Gi’s mystery-organism-code
Hello, this is my work and would like to know if there is any way for me to optimize the code. Mysterious Organism Project
Hi everyone! I’d like to share with you my version of this challenge, it took me some days to do that and even if I didn’t reach to complete the last compareDNA() I’d love to know what do you think of my work!
Thanks!
My Solution:
That was starting to get complicated!
Here’s mine, all comments welcome.
Code: https://github.com/SoCentral2/mysteriousOrganism
Paul.
My Solution:
My solution for this
It does not fully comply the initial task for methods behavior but it was a good one to work on different approaches for using methods and default values.
Hello,
I’ll share here my solution
Hello everyone, I have solved this project as well as for the extra challenge. Please have a look
Mysterious Organism Code
Hi guys, this is my solution.
check my GitHub:
Hi guys, this was a little hard, but I managed to do it with a little help. It’s not pretty but it gets the job done. P.S reviews are always welcome. Ciao!
Hi guys! Here is my solution for the Mysterious Organism project. I know I haven’t written efficient code, so sharing ways to correct or shorten the logic is much appreciated.
// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = ['A', 'T', 'C', 'G']
return dnaBases[Math.floor(Math.random() * 4)]
}
// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = []
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}
//Factory
const pAequorFactory = (num, arrDna) =>{
return{
specimenNum: num,
dna: arrDna,
mutate(){
//Get index, and value of random base
const randBaseIndex = Math.floor(Math.random() * 15);
const randBaseValue = this.dna[randBaseIndex];
//Remove the random base from the bases
const dnaBases = ['A', 'T', 'C', 'G'];
dnaBases.splice(dnaBases.indexOf(randBaseValue), 1);
//Replace value of random index with one random value from the dna bases
this.dna[randBaseIndex] = dnaBases[Math.floor(Math.random() * 3)];
return this.dna;
},
compareDNA(pAequor){
const parts = 15;
let matchCount = 0;
for(let i = 0; i < pAequor.dna.length; i++){
if(pAequor.dna[i] === this.dna[i]){
matchCount++;
}
};
const matchPercent = (matchCount / parts) * 100;
/*console.log(`Specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${Math.round(matchPercent*10)/10}% DNA in common`);*/
return Math.round(matchPercent * 10) / 10
},
willLikelySurvive(){
//60% of 15 bases is 9
let cgCount = 0;
const survivalCount = (60/100) * 15; //9
for(let i = 0; i < this.dna.length; i++){
if(this.dna[i] === 'C' || this.dna[i] === 'G'){
cgCount++;
}
}
return cgCount >= survivalCount ? true: false;
},
complementStrand(){
let compDNAStarnd = this.dna.concat();//Shallow copy
for(let i = 0 ; i < this.dna.length; i++){
switch(compDNAStarnd[i]){
case 'A':
compDNAStarnd[i] = 'T';
break;
case 'T':
compDNAStarnd[i] = 'A';
break;
case 'C':
compDNAStarnd[i] = 'G';
break;
case 'G':
compDNAStarnd[i] = 'C';
break;
}
}
return compDNAStarnd;
}
};
}
//1. Test object creation
console.log('---------Object creation---------');
const dnaBase1 = ['A', 'C', 'T', 'G','A', 'C', 'T', 'G','A', 'C', 'T', 'G','A', 'C', 'T'];
const pAequor1 = pAequorFactory(1, dnaBase1);
console.log(pAequor1);
//2. Test object mutation
console.log('\n---------Object mutation----------');
console.log(pAequor1.mutate());
//3. Test object comparision by creating another object
console.log('\n-------Compare two objects--------');
const dnaBase2 = ['A', 'C', 'T', 'G','A', 'C', 'T', 'G','A', 'C', 'T', 'G','A', 'C', 'T'];
const pAequor2 = pAequorFactory(2, dnaBase2);
//gives (14/15) * 100 = 93.3%, cz pAequor1 has been mutated once, which makes it different from pAequor2.
console.log(`P. Aequor #${pAequor1.specimenNum}: ${pAequor1.dna.join(',')}\nP. Aequor #${pAequor2.specimenNum}: ${pAequor2.dna.join(',')}`)
//pAequor2.compareDNA(pAequor1);
console.log(`Specimen #${pAequor2.specimenNum} and specimen #${pAequor1.specimenNum} have ${ Math.round(pAequor2.compareDNA(pAequor1) * 10) / 10 }% DNA in common`);
//4. Test if P. aequor is will likely to survive
console.log('\n-------Will likely survive--------');
const dnaBase3 = ['A', 'C', 'C', 'C','A', 'C', 'T', 'C','A', 'C', 'T', 'C','A', 'C', 'C'];
const pAequor3 = pAequorFactory(3, dnaBase3);
console.log(`P. Aequor with dna ${pAequor3.dna.join(',')} will likely survive: ${pAequor3.willLikelySurvive()}`);//True - (9 C's)
//5. Test 30 sample objects creation for team study
console.log('\n--------Create 30 instances-------');
const samplePAequors = [];
const instancesCount = 30;
let dnaBase = [];
let pAequor = {};
let pAequorID = 1;
while(samplePAequors.length < instancesCount){
dnaBase = mockUpStrand();
pAequor = pAequorFactory(pAequorID, dnaBase);
if(pAequor.willLikelySurvive()){
samplePAequors.push(pAequor);
pAequorID++;
}
}
console.log(`Created instances: ${samplePAequors.length}`);
//console.log(samplePAequors);
//6. Test complimentary dna starand
console.log('\n-----Complimentary DNA strand-----');
//Lets take instance pAequor1 for example
console.log(`Initial DNA:\n ${pAequor1.dna.join(',')}`);
console.log(`Compliment DNA:\n ${pAequor1.complementStrand().join(',')}`);
//7. Test compare two most related instance out of 30 sample
console.log('\n-----Most related instances-----');
const comparisionResult = [];
//To use compareDNA() the function must return a value
let maxMatchObjects = [];
let currentMatchvalue = 0;
let maxMatchValue = 0;
for(let i = 0; i < samplePAequors.length; i++){
for(let j = i + 1; j < samplePAequors.length; j++){
currentMatchvalue = samplePAequors[i].compareDNA(samplePAequors[j]);
if(currentMatchvalue > maxMatchValue){
maxMatchValue = currentMatchvalue;
maxMatchObjects[0] = samplePAequors[i].specimenNum;
maxMatchObjects[1] = samplePAequors[j].specimenNum;
}
//console.log(samplePAequors[i].specimenNum + ':' + samplePAequors[j].specimenNum + ':' + currentMatchvalue)
}
}
const matchedObjects = samplePAequors.filter(pAequor =>{
return maxMatchObjects.some(objId =>{
return objId === pAequor.specimenNum;
})
})
matchedObjects.forEach(obj =>{
console.log(`ID: ${obj.specimenNum} DNA: ${obj.dna.join(',')}`);
})
console.log(`Match Value: ${maxMatchValue}%`);
You can access my project solution from the below GitHub link.
This is my solution, really learned a lot while working on this. please i will appreciate feedbacks.
My solution!
I was hopelessly stuck on dna.splice(index, 1) versus dna = dna.splice(index, 1)
Hi, reading your code really gave me a different angle to look at the problem like with in the willLikelySurvive() method. I liked how you made a condition to test counter being greater than 9 since 9/15 will be 0.60! But, I believe you made a little error at that part, the condition should be (counter >= 9) since it’s at least 60%.
Hey! Here is the code to my solution for this project