-
-
Save Rruvez/2233bb28419b45ebbceca786894c2cd9 to your computer and use it in GitHub Desktop.
Mysterious Organism: Challenge project on Codecademy
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; | |
}; | |
// a factory function that returns a new object of a pAequor | |
function pAequorFactory(specimenNum, dna) { | |
return { | |
specimenNum, | |
dna, | |
// a method that mutates a randomly selected dna base | |
mutate() { | |
let dnaBases = ['A', 'T', 'C', 'G']; // an array of all available bases | |
const randBase = Math.floor(Math.random() * 15); // randomly chosen base of an existing DNA object | |
const mutateBase = this.dna[randBase]; | |
const randChoice = Math.floor(Math.random() * 3); // randomly chosen base of the available bases | |
const availableBases = dnaBases.filter(base => base !== mutateBase); // exclude the chosen base from the variety | |
this.dna[randBase] = availableBases[randChoice]; | |
}, | |
// a method for comparing 2 dna sequences | |
compareDNA(pAequor) { | |
let match = 0; // counter for all DNA bases that match | |
for (let i = 0; i < this.dna.length; i++) { | |
if (this.dna[i] === pAequor.dna[i]) { | |
match++; | |
} | |
} | |
const percentage = parseInt((match / this.dna.length * 100).toFixed()); // calculate the percentage of similarity | |
return percentage; | |
// return `specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${percentage}% DNA in common`; | |
}, | |
// a method to check whether this specimen has a likelier chance of survival | |
willLikelySurvive() { | |
let match = 0; // counter for all DNA bases that match 'C' or 'G' bases | |
this.dna.forEach(base => {if (base === 'C' || base === 'G') { match++; } | |
}); | |
const percentage = parseInt((match / this.dna.length * 100).toFixed()); | |
return percentage > 60; | |
} | |
} | |
} | |
// Generate 30 specimen that have a likelier chance of survival and store them for the further investigation | |
function generateSurvivals(arr) { | |
let specimenCount = 1; | |
while (specimenCount <= 30) { | |
let newSpecimen = pAequorFactory(specimenCount, mockUpStrand()) | |
if (newSpecimen.willLikelySurvive()) { | |
arr.push(newSpecimen); | |
specimenCount++; | |
} | |
} | |
} | |
// Return the most related instances from the list of 30 specimen generated with generateSurvivals() | |
function findRelatedDNA(obj) { | |
let firstRelated; // first of 2 most related objects | |
let secondRelated; // second of 2 most related objects | |
let highestSimilarity = 0; | |
for (let i = 0; i < obj.length - 1; i++) { | |
for (let j = i + 1; j < obj.length; j++) { | |
let obj1 = obj[i]; | |
let obj2 = obj[j]; | |
let percentage = obj1.compareDNA(obj2) | |
if (percentage > highestSimilarity) { | |
highestSimilarity = percentage; | |
firstRelated = obj1.specimenNum; | |
secondRelated = obj2.specimenNum; | |
} | |
} | |
} | |
return `Specimen ${firstRelated} and ${secondRelated} have the highest similarity among the list of ${obj.length} specimen: ${highestSimilarity}%`; | |
} | |
// mutate() CAPACITY CHECK | |
// let newObj = pAequorFactory(1, mockUpStrand()); | |
// console.log(newObj.dna); | |
// newObj.mutate(); | |
// console.log(newObj.dna); | |
// compareDNA() CAPACITY CHECK | |
// let newObj1 = pAequorFactory(1, mockUpStrand()); | |
// let newObj2 = pAequorFactory(2, mockUpStrand()); | |
// console.log(newObj1.dna); | |
// console.log(newObj2.dna); | |
// console.log(newObj1.compareDNA(newObj2)); | |
// willLikelySurvive() CAPACITY CHECK | |
// let newObj1 = pAequorFactory(1, mockUpStrand()); | |
// console.log(newObj1.dna) | |
// console.log(newObj1.willLikelySurvive()) | |
// generateSurvivals() CAPACITY CHECK | |
// specimenArr = []; | |
// generateSurvivals(specimenArr); | |
// console.log(specimenArr); | |
// findRelatedDNA() CAPACITY CHECK (in order to get results uncomment generateSurvivals() as well) | |
// console.log(findRelatedDNA(specimenArr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment