Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 16, 2020 23: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/db4ab40f74c3b02406c106677bbbcca5 to your computer and use it in GitHub Desktop.
Save codecademydev/db4ab40f74c3b02406c106677bbbcca5 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
}
function pAequorFactory(num, arr){
return{
_specimenNum: num,
_dna: arr,
get specimenNum(){
return this._specimenNum;
},
get dna(){
return this._dna;
},
set specimenNum(newSpecimenNum){
return this._specimenNum = newSpecimenNum;
},
set dna(newDna){
return this._dna = newDna;
},
mutate(){
console.log('DNA: \n' + this._dna);
let pos = Math.floor(Math.random() * 15);
console.log('Base position randomly selected: ' + pos + '\nChosen base: ' + this._dna[pos]);
while(true){
randBase = returnRandBase();
if(!(this._dna[pos] === randBase)){
break;
}
}
console.log('Random Base Selected (different than the chosen one): ' + randBase);
this._dna[pos] = randBase;
console.log('New mutated DNA: ' + this._dna);
},
compareDNA(obj){
let percentage = 0;
for(let i = 0; i < this._dna.length; i++){
if(this._dna[i] === obj._dna[i]){
percentage++;
}
}
return percentage * 100/15;
},
willLikelySurvive(){
let likelyhood = 0;
for(element in this._dna){
if(this._dna[element] === 'C' || this._dna[element] === 'G'){
likelyhood++;
}
}
console.log('Likelyhood: ' + likelyhood * 100/15 + '%')
if((likelyhood * 100/15) >= 60){
return true;
}else{
return false;
}
},
complementStrand(){
let complement = [];
for(element in this._dna){
if(this._dna[element] === 'A'){
complement.push('T');
}
else if(this._dna[element] === 'T'){
complement.push('A');
}
else if(this._dna[element] === 'C'){
complement.push('G');
}
if(this._dna[element] === 'G'){
complement.push('C');
}
}
return 'DNA received: ' + this._dna + '\nComplement: \n' + complement;
}
}
}
let organism1 = pAequorFactory(1, mockUpStrand());
//organism1.mutate();
let survivors = [];
let index = 0;
let control = 0;
while(survivors.length < 30){
organism1 = pAequorFactory(index, mockUpStrand());
console.log('Hi, I am N#: ' + control);
control++;
console.log('Will i survive? ' + organism1.willLikelySurvive());
if(organism1.willLikelySurvive()){
survivors.push(organism1);
console.log('Yay, i was added! \nMy index is: ' + index);
console.log('My DNA is: ' + organism1.dna + '\n');
index++;
}
console.log('\n');
}
for(element in survivors){
console.log(survivors[element].specimenNum);
console.log(survivors[element].dna);
}
console.log('\n \n');
console.log('The two most related survivors pAqueor are: ');
let resemblance = 0;
let arr = [];
for(let i = 0; i < survivors.length; i++){
for(let j = 1; j < survivors.length; j++){
if(i !== j && i < j){
let comparison = [];
resemblance = survivors[i].compareDNA(survivors[j]);
console.log('N# ' + survivors[i].specimenNum);
console.log('N# ' + survivors[j].specimenNum);
console.log('Resemblance percentage: ' + resemblance + '%\n');
comparison.push(survivors[i].specimenNum, survivors[j].specimenNum,resemblance);
arr.push(comparison);
}
}
}
let related = 0;
for(let i = 0; i < arr.length; i++){
if(related < arr[i][2]){
related = arr[i][2];
}
if(related === 100){
break;
}
}
let highRel = 0;
console.log('Highest Resemblance Percentage: ' + related +'%');
console.log('\n\nThe two most related survivor instances of pAqueor are: ');
console.log('(Specimen 1 | Specimen 2 | Related Percentage)');
for(elem in arr){
if(arr[elem][2] === related){
console.log(arr[elem].join(' | '));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment