Second time I’ve done this challenge after one year and half. Very fun! I’ve improved in some areas and left you here my code with some extra code
// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = ["A", "T", "C", "G"];
return dnaBases[Math.floor(Math.random() * 4)];
};
// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = [];
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase());
}
return newStrand;
};
// Store Specimen Numbers in use
const usedSpecimenNum = [];
// Creates objects
const pAequorFactory = (num) => {
// Check if Specimen Number it's in use
if (usedSpecimenNum.includes(num)) {
console.log(
`Error! Use a different Specimen Number, ${num} it's allready in use!`
);
return null;
} else {
console.log("Specimen Number " + num + " created successfully!");
}
// Add num to array of Specimen Numbers
usedSpecimenNum.push(num);
return {
_specimenNum: num,
dna: mockUpStrand(),
mutate() {
// Select random index on dna
const randomIndex = Math.floor(Math.random() * this.dna.length);
// Replace random Base
let newBase = returnRandBase();
while (newBase === this.dna[randomIndex]) {
newBase = returnRandBase();
}
// Replace dna base
this.dna[randomIndex] = newBase;
// Return DNA
return this.dna;
},
compareDNA(other) {
// Retreive DNA array from other object
const otherDNA = other.dna;
let identicalBases = 0;
// Check DNA in common
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === otherDNA[i]) {
identicalBases++;
}
}
// Check percentage DNA in commmon
const identicalPercentage = (identicalBases / this.dna.length) * 100;
// Round percentage
const roundedPercentage = identicalPercentage.toFixed(1);
// Logs message to console
console.log(
`Specimen ${this._specimenNum} and Specimen ${other._specimenNum} has ${roundedPercentage} % DNA in common.`
);
},
willLikelySurvive() {
// Count how many C and G bases on DNA
const countCG = this.dna.filter(
(base) => base === "C" || base === "G"
).length;
// Check percentage of C and G bases
const percentageCG = (countCG / this.dna.length) * 100;
this._survivalPercentage = percentageCG.toFixed(1);
return percentageCG >= 60
},
complementStrand() {
// Returns complementary DNA strand where 'A' match 'T' and 'C' match 'G' vice-versa
const complementDNA = [];
for (let i = 0; i < this.dna.length; i++) {
switch (this.dna[i]) {
case 'A' :
complementDNA.push('T');
break;
case 'T' :
complementDNA.push('A');
break;
case 'C' :
complementDNA.push('G');
break;
case 'G' :
complementDNA.push('C');
break;
default:
console.log('Wrong base!')
break;
}
}
return complementDNA
},
};
};
/*
// Creating 30 instances and store them in an array to be study later
const pAequorArray = [];
for (let i = 0; i < 30; i++) {
pAequorArray.push(pAequorFactory([i]))
}
// console.log(pAequorArray);
*/
// Uncomment testing to check results
// Comment out the loop that creates 30 instances for only test resulst will appear in console.
// Testing unique ID functionality
const pAequor1 = pAequorFactory(1);
const pAequor2 = pAequorFactory(1);
const pAequor3 = pAequorFactory(3);
// Testing Mutate method
function testMutate() {
const pAequor = pAequorFactory(4);
// Store original DNA
const originalDNA = [...pAequor.dna];
// Mutate DNA
const mutated = pAequor.mutate();
// Count mutations
let mutationCount = 0;
for (let i = 0; i < originalDNA.length; i++) {
if (originalDNA[i] !== mutated[i]) {
mutationCount++;
console.log(
`DNA mutated at position: ${i}: originalDNA: ${originalDNA[i]} mutated for: ${mutated[i]}!`
);
}
}
// Check if only one base was mutated
if (mutationCount === 1) {
console.log("Only 1 base mutated, method works correctly");
} else {
console.log(
"More them one base mutated! Method isn't working like it should!!!"
);
}
}
testMutate();
// Testing compareDNA method
pAequor1.compareDNA(pAequor3);
// Testing willLikelySurvive method
function testWillLikelySurvive() {
const pAequor5 = pAequorFactory(5);
console.log('pAequor will survive if chances are higher them 60%')
if(pAequor5.willLikelySurvive() > 59) {
console.log('Survival percentage of: ' + pAequor5._survivalPercentage + '% pAequor will survive!' )
} else {
console.log('Survival percentage of: ' + pAequor5._survivalPercentage + '% pAequor will not survive!' )
}
pAequor5.willLikelySurvive();
}
testWillLikelySurvive()
function testComplementStrand(specimen) {
const originalDNA = specimen.dna;
const complementDNA = specimen.complementStrand();
console.log('Checking if complementary DNA it\'s correct...');
let result = true;
for (let i = 0; i < originalDNA.length; i++) {
const originalBase = originalDNA[i];
const complementBase = complementDNA[i];
switch (originalBase) {
case 'A' :
if (complementBase !== 'T') {
result === false;
}
break
case 'T' :
if (complementBase !== 'A') {
result === false;
}
break
case 'C' :
if (complementBase !== 'G') {
result === false;
}
break
case 'G' :
if (complementBase !== 'C') {
result === false;
}
break
default:
console.log('Wrong base!')
break;
}
};
if (result) {
console.log('All bases match correctly')
} else {
console.log('Wrong matches! DNA vulnerable!')
}
}
testComplementStrand(pAequor1);
This repository contains JavaScript code simulating the creation, mutation, survival, and comparison of hypothetical organisms named P. aequor.
Key Features:
P. aequor Object: Represents an organism with properties like specimen number, DNA sequence, and methods for mutation, DNA comparison, survival likelihood, and complement strand generation.
DNA Simulation: Generates random DNA sequences and simulates mutations.
Survival Analysis: Determines organism viability based on DNA composition.
DNA Comparison: Calculates DNA similarity between two organisms.
Usage:
pAequorFactory(specimenNum, dna) : Creates a new P. aequor instance.
createSurvivingpAequor(numpAequor) : Generates a specified number of surviving P. aequor instances.
findMostRelated(pAequorPopulation) : Finds the two most related P. aequor instances from a population.
that javaSCRIPT, not JAVA, that’s totally a different language. however, if you meant javascript, I don’t mind to try helping you with specific questions. anyway, I’m sorry for the misunderstanding.
Hello! I stepped away from this project a while ago because it was tough for me. With no live guidance, I really struggle. However, I used ChatGPT as a mentor this time, and I’m happy with the results! (I ensured ChatGPT would not give me the full answer to a question - only guidance.) Please tell me what you think!
// Returns a random DNA base, which is represented by A, T, C, G
const returnRandBase = () => {
const dnaBases = ['A', 'T', 'C', 'G'];
return dnaBases[Math.floor(Math.random() * 4)];
}
// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = [];
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase());
}
return newStrand;
}
const pAequorFactory = (num, arr) => {
return {
specimenNum: num,
dna: arr,
mutate() {
// Obtains a random index from the inputted dna array, which will be replaced
const randomIndex = Math.floor(Math.random() * this.dna.length);
let currentBase = this.dna[randomIndex];
// Introduces newBase, & do...while ensures it is not the same base as currentBase
let newBase;
do {
newBase = returnRandBase();
} while (currentBase === newBase);
// Assigns newBase to currentBase's index & returns the dna array/property
this.dna[randomIndex] = newBase;
return this.dna;
},
compareDNA(otherDNA) {
// Setup
const dna1 = this.dna;
const dna2 = otherDNA.dna;
let matchCount = 0;
// Comparison
for (let i = 0; i < dna1.length; i++) {
if (dna1[i] === dna2[i]) {
matchCount++;
}
}
// Return note & conversion to a percentage
const percentage = (matchCount / this.dna.length) * 100;
return `Specimen #${this.specimenNum} & Specimen #${otherDNA.specimenNum} have ${percentage.toFixed(2)}% DNA in common.`;
},
willLikelySurvive() {
const dna = this.dna;
const totalBases = dna.length;
const cgCount = dna.filter(base => base === 'C' || base === 'G').length;
const percentageCG = (cgCount / totalBases) * 100;
return percentageCG >= 60;
}
}
}
// Organism #1
const organism = pAequorFactory(1, mockUpStrand());
// mutate() Test - SUCCESS
/* console.log("Before Mutation:", organism.dna);
organism.mutate();
console.log("After Mutation:", organism.dna); */
// Organism #2
const organism2 = pAequorFactory(2, mockUpStrand());
// compareDNA() TEST - between 1 & 2 - SUCCESS
// console.log(organism.compareDNA(organism2));
// willLikelySurvive() TEST - 1 & 2 - SUCCESS
/* console.log("#1 is likely to survive: " + organism.willLikelySurvive());
console.log("#2 is likely to survive: " + organism2.willLikelySurvive()); */
// Acquiring 30 instances of organisms that are likely to survive
const likelySamples = [];
let specimenCounter = 1;
while (likelySamples.length < 30) {
const org = pAequorFactory(specimenCounter, mockUpStrand());
if (org.willLikelySurvive()) {
likelySamples.push(org);
}
specimenCounter++;
}
// Testing to ensure 30 instances - SUCCESS
/* console.log("Here are " + likelySamples.length + " promising samples eligible for further study:");
console.log(likelySamples); */
// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = [‘A’, ‘T’, ‘C’, ‘G’]
return dnaBases[Math.floor(Math.random() * 4)]
}
// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand =
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}
let ex2 = mockUpStrand();
const pAequorFactory = (numb,dnaArray) => {
return {
_specimenNum: numb,
_dna: dnaArray,
mutate () {
let i = Math.floor(Math.random() * dnaArray.length);
let mutateDna = this._dna[i];
let randomDna = returnRandBase();
if (mutateDna !== randomDna) {
this._dna[i] = randomDna;
}
return this._dna;
},
compareDNA () {
let compareSum = 0;
for(let x = 0; x < this._dna.length; x++) {
if (this._dna == ex2) {
compareSum++;
}
}
return ((compareSum/15) * 100).toFixed(2) + “%”;
},
willLikelySurvive () {
let surviveDna = 0;
for (let y = 0; y < this._dna.length; y++){
if (this._dna[y] == ‘G’ || this._dna[y] == ‘C’){
surviveDna++;
}
}
surviveDna = (surviveDna/15) *100
if (surviveDna >= 60) {
return surviveDna;
} else {
return surviveDna;
}
},
complementStrand () {
let complementDna = ;
for (z = 0; z < this._dna.length; z++) {
switch (this._dna[z]) {
case ‘A’: complementDna.push(‘T’)
break;
case ‘T’: complementDna.push(‘A’)
break;
case ‘C’: complementDna.push(‘G’)
break;
case ‘G’: complementDna.push(‘C’)
break;
default: complementDna.push(‘Unknown’)
}
}
return complementDna;
}
};
};
let ex = pAequorFactory(1,mockUpStrand());
console.log(ex);
console.log(ex.mutate());
console.log(ex.compareDNA());
console.log(ex.willLikelySurvive());
console.log(ex.complementStrand());