Skip to content

Instantly share code, notes, and snippets.

@hypyeon

hypyeon/main.js Secret

Created December 5, 2020 10:34
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 hypyeon/18267a5f575db86ae963433ec70555ee to your computer and use it in GitHub Desktop.
Save hypyeon/18267a5f575db86ae963433ec70555ee to your computer and use it in GitHub Desktop.
// 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
}
const pAequorFactory = (specimenNum, dna) => {
let object = {
number: specimenNum,
DNA: dna,
mutate() {
const randomBase = Math.floor(Math.random() * dna.length);
const removed = dna.remove(randomBase);
const newBase = Math.floor(Math.random() * removed.length);
return newBase;
},
compareDNA(pAequorObj) {
let numOfCommon = 0;
for (let i = 0; i < dna.length; i++) {
if (pAequorObj.dna[i] === this.dna[i]) {
numOfCommon += 1
}
};
const percentage = (numOfCommon / 15 * 100).toFixed(2);
return percentage;
console.log(`Specimen 1 and Specimen 2 have ${percentage} DNA in common.`);
},
willLikelySurvive() {
let cgBases = 0;
for (let i = 0; i < this.DNA.length; i++) {
if (this.DNA[i] === 'C' || this.DNA[i] === 'G') {
cgBases += 1
}
};
const sixtyPer = 15 / 10 * 6;
return cgBases >= sixtyPer ? true : false
}
};
return object
};
const create30 = () => {
let pAequorArray = [];
let i = 1;
const newDNA = pAequorFactory(i, mockUpStrand());
const surv = newDNA.willLikelySurvive();
if (surv === true) {
i += 1
do {
pAequorArray.push(newDNA);
} while (pAequorArray.length < 30);
}
return pAequorArray
};
//test:
// console.log(pAequorFactory(1, mockUpStrand()));
console.log(create30())
/* The result shows either empty array ([]) or something like this:
[ { number: 1,
DNA: [ 'G', 'T', 'T', 'C', 'T', 'G', 'C', 'C', 'G', 'C', 'C', 'G', 'A', 'T', 'A' ],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive] }, ... ] ( 30 objects in total)
I succeeded in printing out only 'surviving' DNAs using willLikelySurvive() function, but how can I get result with
amounting numbers for 'number' (my current result shows '1' for all objects), also, is it normal to just have
"mutate: [Function: mutate]" printed like that? Please help........
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment