-
-
Save codecademydev/bbc8cc63d8ff145b9cb49bfaa4ef11ca to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
//Creates a factory function for P. Aequor with the properties of specimenNum, dna and three methods: mutate, compareDNA and willLikelySurvive | |
const pAequorFactory = (number,array) =>{ | |
if (typeof(number) === 'number' && Array.isArray(array)){ | |
return { | |
specimenNum: number, | |
dna: array, | |
// This method checks for a random index value and generates random bases, if they are the same then it changes to a new base and not the one before | |
mutate(){ | |
const currentBase = this.dna[Math.floor(Math.random()* 15)]; | |
const newBase = returnRandBase(); | |
if (currentBase == newBase){ | |
switch(newBase){ | |
case 'A': | |
this.dna[this.dna.indexOf(currentBase)] = 'T'; | |
break | |
case 'T': | |
this.dna[this.dna.indexOf(currentBase)] = 'C'; | |
break | |
case 'C': | |
this.dna[this.dna.indexOf(currentBase)] = 'G'; | |
break | |
case 'G': | |
this.dna[this.dna.indexOf(currentBase)] = 'A'; | |
break | |
default: | |
console.log('No mutation') | |
} | |
} else { | |
this.dna[this.dna.indexOf(currentBase)] = newBase; | |
} | |
return this.dna | |
}, | |
//This method compares two stands of dna and if they have in the same index the same base, this function makes a operation to get the percentage that the two dna bases shares | |
compareDNA(pAequor, specimenNum){ | |
let count = 0; | |
for (i=0; i < this.dna.length; i++){ | |
if (this.dna[i]==pAequor[i] && this.dna[this.dna.indexOf(i)] == pAequor[pAequor.indexOf(i)]){ | |
count += 1; | |
} | |
} | |
let percentage = (count/pAequor.length)*100; | |
return `specimen ${this.specimenNum} and specimen ${specimenNum} have ${percentage.toFixed(2)}% DNA in common.` | |
}, | |
//This method measures the 'C' and 'G' bases, then makes a count and returns true if the operation has more than 60% of these bases or false if it is lower than 60% | |
willLikelySurvive(){ | |
let count = 0; | |
for(i=0; i < this.dna.length; i++){ | |
if(this.dna[i]==='C'||this.dna[i]==='G'){ | |
count +=1; | |
} | |
} | |
let percentage = (count/this.dna.length)*100; | |
if (percentage.toFixed(2) >= 60){ | |
return true | |
} else { | |
return false | |
} | |
} | |
} | |
} else { | |
return 'Not valid Specimen'; | |
} | |
}; | |
//This function expression returns an array of 30 p. Aequor objects that will survive in it's enviroment | |
const factoryCreator = () => { | |
let newArray = []; | |
let count = 1; | |
let i = 0; | |
do{ | |
newArray.push(pAequorFactory(count, mockUpStrand())); | |
newArray[i].willLikelySurvive(); | |
if (newArray[i].willLikelySurvive() === true) { | |
count += 1; | |
i += 1; | |
} else if (newArray[i].willLikelySurvive() === false){ | |
newArray.pop(); | |
} | |
} while (i < 30); | |
return newArray; | |
}; | |
console.log('This are the p. Aequor\'s that will survive: \n'); | |
console.log(factoryCreator()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you did a real nice job. I like everything except the "do" statement as I don't use it a lot. Here is my variation
Also I know it doesn't say it in the instructions but I made mine so that the 30 instances in the final array are all unique versions of the specimen. I don't believe yours accounts for this. Could be a fun re-write.