Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created Feb 1, 2020
Embed
What would you like to do?
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 strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = []
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}
// Factory Functions
let pAequorFactory = (num, dnaBase) => {
return {
specimenNum: num,
dna: dnaBase,
mutate() {
let index = Math.floor(Math.random() * this.dna.length);
let value = returnRandBase();
do {
this.dna[index] = value;
} while (value === this.dna[value]);
},
compareDNA(object) {
let object1 = this.dna;
let object2 = object.dna;
let total = 0;
for (let i=0; i<15; i++) {
for (let j=0; j<15; j++) {
if(i === j && object1[i] === object2[j]) {
total += 6.67;
}
}
}
return `This object shares a ${total}% dna affinity with ${object}`;
},
willLikelySurvive() {
let gTotal = 0;
let cTotal = 0;
let willSurvive;
for (let i=0; i<15; i++) {
if (this.dna[i] === 'G') {
gTotal += 6.67;
} else if (this.dna[i] === 'C') {
cTotal += 6.67;
}
}
if (gTotal >= 60 || cTotal >= 60) {
return true;
} else {
return false;
}
},
complementStrand() {
let obj1 = [];
let obj2 = this.dna;
for (let i=0; i<15; i++) {
if (obj2[i] === 'A') {
obj1.push('T');
} else if (obj2[i] === 'T') {
obj1.push('A');
} else if (obj2[i] === 'G') {
obj1.push('C');
} else if (obj2[i] === 'C') {
obj1.push('G');
}
return obj1;
}
}
}
}
let one = pAequorFactory(1, mockUpStrand());
console.log(one.dna);
console.log(one.complementStrand());
const survivorArr = [];
let idCounter = 1;
while (survivorArr.length < 30) {
let newOrganism = pAequorFactory(idCounter, mockUpStrand());
if (newOrganism.willLikelySurvive()) {
survivorArr.push(newOrganism);
};
idCounter ++;
}
// console.log(survivorArr);
// let one = pAequorFactory(1, mockUpStrand());
// console.log(one.dna);
// one.willLikelySurvive();
// let one = pAequorFactory(1, mockUpStrand());
// console.log(one.dna);
// let two = pAequorFactory(2, mockUpStrand());
// console.log(two.dna);
// console.log(one.compareDNA(two));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment