-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganisms.js
More file actions
63 lines (53 loc) · 1.72 KB
/
organisms.js
File metadata and controls
63 lines (53 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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) => {
return{
specimenNum,
dna,
mutate() {
const randIndex = Math.floor(Math.random() * this.dna.length);
let newBase = returnRandBase();
while (this.dna[randIndex] === newBase) {
newBase = returnRandBase();
}
this.dna[randIndex] = newBase;
return this.dna;
},
compareDna(otherOrg) {
const similarities = this.dna.reduce((acc, curr, idx, arr) => {if (arr[idx] === otherOrg.dna[idx]) {
return acc + 1;
} else {
return acc;
}
}, 0);
const percentOfDNAshared = (similarities / this.dna.length) * 100;
const percentageTo2Deci = percentOfDNAShared.toFixed(2);
console.log(`${this.specimenNum} and ${otherOrg.specimenNum} have ${percentageTo2Deci}% DNA in common.`);
},
willLikelySurvive(){
const cOrG = this.dna.filter(el => el === "C" || el === "G");
return cOrG.length / this.dna.length >= 0.6;
},
}
};
const survivingSpecimen = [];
let idCounter = 1;
while (survivingSpecimen.length < 30) {
let newOrg = pAequorFactory(idCounter, mockUpStrand());
if(newOrg.willLikelySurvive()) {
survivingSpecimen.push(newOrg);
}
idCounter++;
}
console.log(survivingSpecimen)