Mysterious Organism Challenge Project (JavaScript)

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 :smiley:

// 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);
1 Like

Here the link to my solution: DNA app project Codecademy · GitHub

Happy coding!

P. aequor Simulation

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.

Hello,
It was really challenge for me and I learned a lot about objects.
Here I send a link to my code, looking forward to some comments.

Regards
Pawel