-
-
Save codecademydev/07417eddd1fb1b540041ba644ec7284f 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; | |
}; | |
const pAequorFactory = (specimenNum, dna) => { | |
return { | |
specimenNum, | |
dna, | |
// changes a single random base | |
mutate () { | |
const index = Math.floor(Math.random() * this.dna.length); | |
let oldBase = this.dna[index]; | |
let newBase = ''; | |
//ensures the base is different NOT WORKING | |
do { | |
newBase = returnRandBase(); | |
} | |
while (newBase === oldBase); | |
this.dna[index] = newBase; | |
}, | |
//Compares the similarity of the strands | |
compareDNA (pAequor) { | |
let numEqualPairs = 0; | |
if (pAequor.dna.length !== this.dna.length) { | |
console.log('DNA strands are different lengths'); | |
} else { | |
for (let i = 0; i < this.dna.length; i++) { | |
if(pAequor.dna[i] === this.dna[i]) { | |
numEqualPairs++; | |
} | |
} | |
const percentAlike = Math.round(numEqualPairs/this.dna.length*100); | |
console.log(`Specimen #${this.specimenNum} and Specimen #${pAequor.specimenNum} have ${percentAlike}% DNA in common`); | |
} | |
}, | |
//tests if 60% or more of pairs are 'C' or 'G' | |
willLikelySurvive () { | |
let numCG = 0; | |
//Counts C or G bases | |
for (let i = 0; i < this.dna.length; i++) { | |
if (this.dna[i] === 'C' || this.dna[i] === 'G') { | |
numCG++; | |
} | |
} | |
console.log(numCG +' '+ numCG/this.dna.length); | |
if (numCG/this.dna.length >= 0.6) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
}; | |
//Function creates N organisms likely to survive returns array of objects | |
function createCreatures(num) { | |
let organisms = []; | |
let numSurvived = 0; | |
do { | |
let sampCreature = pAequorFactory(numSurvived + 1, mockUpStrand()); | |
if(sampCreature.willLikelySurvive) { | |
organisms.push(sampCreature); | |
numSurvived++; | |
} | |
} while (numSurvived < num); | |
return organisms; | |
} | |
creatures = createCreatures(30); | |
console.log(creatures); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment