Skip to content

Instantly share code, notes, and snippets.

@alexanderverheijden
Created February 8, 2022 21:03
Show Gist options
  • Save alexanderverheijden/ada584405c50bf25dea966cb202d30c7 to your computer and use it in GitHub Desktop.
Save alexanderverheijden/ada584405c50bf25dea966cb202d30c7 to your computer and use it in GitHub Desktop.
Challenge Project mysterious species
// 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
};
let specimens = [];
let mutSpecimens = [];
let canSurvive = [];
// Creation of sequences
const pAequorFactory = (nr, array) => {
const specimen = {specimen: nr,
dna: array}
specimens.push(specimen);
};
// Mutation of sequences
const mutate = (nr) => {
const randBase = Math.floor(Math.random() * 15);
const mutatedBase = randBase + 1;
console.log('The mutated base sequence of DNA: ' + mutatedBase);
let newBase = '';
const specimensCopy = specimens[nr-1].dna;
const string = specimensCopy.join(' ');
let newS = string.split(' ');
//console.log('this are', specimens);
if (newS[randBase] === 'A') {
const dnaBases = ['T', 'C', 'G'];
newBase = dnaBases[Math.floor(Math.random() * 3)];
} else if (newS[randBase] === 'T') {
const dnaBases = ['A', 'C', 'G'];
newBase = dnaBases[Math.floor(Math.random() * 3)];
} else if (newS[randBase] === 'C') {
const dnaBases = ['A', 'T', 'G'];
newBase = dnaBases[Math.floor(Math.random() * 3)];
} else {
const dnaBases = ['A', 'T', 'C'];
newBase = dnaBases[Math.floor(Math.random() * 3)];
};
newS[randBase] = newBase;
const temp = {specimen: nr,
dna: newS}
mutSpecimens.push(temp);
};
const compareDNA = (obj, mutObj, nr) => {
let counter = 0;
for ( let i = 0 ; i < obj[nr-1].dna.length ; i++) {
if (obj[nr-1].dna[i] === mutObj[nr-1].dna[i] ) {
counter += 1;
};
};
console.log(counter)
const percentage = (100/obj[nr-1].dna.length)*counter;
return 'The two DNA samples are: ' + percentage + '% in common!'
};
const willLikelySurvive = (obj) => {
let countSurvive = 0;
for ( let objNR = 0; objNR < obj.length; objNR++) { let counter = 0;
for ( let i = 0 ; i < obj[objNR].dna.length ; i++) {
if (obj[objNR].dna[i] === 'C' || obj[objNR].dna[i] === 'G') {
counter += 1;
};
};
const percentage = (100/obj[objNR].dna.length)*counter;
if ( percentage >= 60) {
countSurvive += 1;
canSurvive.push(obj[objNR]);
/*return true;
} else {
return false; */
};
if (countSurvive === 30) {
break;
}
};
};
/* create and mutate specimens */
const createNbrs = nr => {
for (let i = 1; i <= nr; i++) {
pAequorFactory(i, mockUpStrand());
};
};
createNbrs(100);
willLikelySurvive(specimens);
console.log('pAequor that can survive:', canSurvive.length)
console.log(canSurvive);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment