Skip to content

Instantly share code, notes, and snippets.

@kriradevska
Last active November 9, 2021 22:45
Show Gist options
  • Save kriradevska/081d5555c1aad56763cd39fd08f1927e to your computer and use it in GitHub Desktop.
Save kriradevska/081d5555c1aad56763cd39fd08f1927e to your computer and use it in GitHub Desktop.
Code Cademy Mysterious Organism Project
// 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
}
//finction to create multiple objects
const pAequorFactory = (number, dna) => {
return ({
specimenNum: number,
dna: dna,
//method to create mutation
mutate() {
let i = Math.floor(Math.random()) * this.dna.lenght;
let newBase = returnRandBase();
while (this.dna[i] === newBase) {
newBase = returnRandBase();
}
this.dna[randIndex] = newBase;
return this.dna;
},
//method to compare DNA between two objects
compareDNA (otherObj) {
let identicalBases = 0;
for (j=o; j<this.dna.lenght; j++) {
if (this.dna[j] === this.otherObj[j]) {return identicalBases++;
} else {
return identicalBases;
}
}
let precent = (identicalBases / this.dna.length) * 100;
console.log(`Specimen #1 and specimen #2 have ${percent.toFixed(2)} DNA in common.`)
},
//method to calculate likelier chance of survival
willLikelySurvive() {
let base = this.dna.filter(element => element==='C' || element==='G');
return base.length / this.dna.length >= 0.6;
},
})
};
// to create new instances of the organism
const newInstances = [];
let idCounter = 1;
while (idCounter < 30) {
let newOrganism = pAequorFactory(idCounter, mockUpStrand());
if (newOrganism.willLikelySurvive()) {
newInstances.push(newOrganism)
}
idCounter++
};
console.log(newInstances);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment