Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created December 5, 2021 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/136f2deec1f0e7d6955f679bfacbdd68 to your computer and use it in GitHub Desktop.
Save codecademydev/136f2deec1f0e7d6955f679bfacbdd68 to your computer and use it in GitHub Desktop.
Codecademy export
// 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
}
// Answer starts here
const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum,
dna,
mutate() {
const x = Math.floor(Math.random() * this.dna.length)
let y = this.dna[x]
let z;
do {
z = returnRandBase()
} while (y == z);
this.dna[x] = z
return this.dna
},
compareDNA(pAequor) {
let commonDNA = []
this.dna.forEach((element, i) => {
if (element === pAequor[i]) {
commonDNA.push(element)
}
})
console.log(`specimen #1 and specimen #2 have ${Math.floor((commonDNA.length / this.dna.length * 100))}% in common`)
},
willLikelySurvive() {
let bases = []
let survivalRate = bases.length/this.dna.length
this.dna.forEach(element => {
if (element === 'C' || element === 'G') {
bases.push(element)
}
})
if (bases.length/this.dna.length >= 0.6) {
return true
} else {
return false
}
}
}
}
const teamRequest = () => {
let thirty = []
let i = 1
let pAequor;
while (thirty.length != 30) {
pAequor = pAequorFactory(i, mockUpStrand())
if (pAequor.willLikelySurvive() === true) {
thirty.push(pAequor)
i++
}
}
return thirty
}
console.log(teamRequest())
// console.log(pAequorFactory(1, mockUpStrand()).mutate())
// pAequorFactory(1, mockUpStrand()).compareDNA(['A', 'C', 'T', 'G', 'C', 'A', 'T', 'T', 'C', 'A', 'T', 'T', 'C', 'A', 'T'])
// console.log(pAequorFactory(1, mockUpStrand()).willLikelySurvive())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment