Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created June 29, 2020 21:47
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/ee92eefd097fd2334d94a52b75dea1bc to your computer and use it in GitHub Desktop.
Save codecademydev/ee92eefd097fd2334d94a52b75dea1bc 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;
};
//factory function (make new specimen)
function pAequorFactory(number, array) {
let object = {
specimenNum: number,
dna: array,
complementStrand() {
let complementaryStrand = [];
this.dna.forEach(function(base) {
if (base === 'A') {
complementaryStrand.push('T');
} else if (base === 'T') {
complementaryStrand.push('A');
} else if (base === 'C') {
complementaryStrand.push('G');
} else if (base === 'G') {
complementaryStrand.push('C');
}
})
return complementaryStrand;
},
mutate() {
let baseToMutate = Math.floor(Math.random() * 15);
let originalBase = this.dna[baseToMutate];
function mutation() {
const dnaBases = ['A', 'T', 'C', 'G'];
dnaBases.splice((dnaBases.indexOf(originalBase)), 1);
return dnaBases[Math.floor(Math.random() * 3)];
}
let mutatedBase = mutation();
this.dna[baseToMutate] = mutatedBase;
return this.dna;
},
compareDNA(pAequor) {
let counter = 0;
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === pAequor.dna[i]) {
counter++;
}
}
let percentage = (counter/this.dna.length) * 100;
// console.log(`Specimen #${this.specimenNum} and Specimen #${pAequor.specimenNum} have ${percentage}% DNA in common.`);
return percentage;
},
willLikelySurvive() {
let counter = 0;
this.dna.forEach(function(element) {
if (element === 'C' || element === 'G') {
counter++;
}
})
let percentage = counter/this.dna.length * 100;
// console.log(`Specimen #${this.specimenNum} has ${percentage}% of 'C' or 'G' in DNA. It's better likelyhood to survive is:`);
return percentage >= 60;
}
};
return object;
};
//make a random population that is likely to survive
function makePAequorPopulation(populationNumber) {
let array = [];
for (i = 1; i < populationNumber+1; i++) {
let individual = pAequorFactory(i, mockUpStrand());
if (individual.willLikelySurvive() === true) {
array.push(individual);
} else {
i--;
}
}
return array;
}
//find two most related specimens
function mostRelated(array) {
let strongestRelations = [];
let mostRelated = 0;
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
let comparison = array[i].compareDNA(array[j]);
if (comparison > mostRelated) {
mostRelated = comparison;
strongestRelations = [(array[i].specimenNum), (array[j].specimenNum)];
} else if (comparison === mostRelated) {
strongestRelations.push((array[i].specimenNum), (array[j].specimenNum));
}
}
}
console.log(`The most related specimens have ${mostRelated}% DNA in common`);
console.log('These are the most related specimens:')
return strongestRelations;
}
let population = makePAequorPopulation(30);
// console.log(population);
console.log(population[1].dna)
console.log(population[1].complementStrand());
console.log(mostRelated(population));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment