Mysterious Organism Challenge Project (JavaScript)

My solution to the challenge

My snippet below!

this is my code !

Here’s my version. Once again, likely a little slower than the solution code.

Here is my solution, i do believe it needs some work on the output tho as it only outputs the function name rather than the actual values.

git: GitHub - eliazar-lopez/Mysterious_Organiism_Challenge: CodeCademy Challenge project in JavaScript

My final code (though I didn’t do the extra challenges)

it seems i do not have the share icon as indicated here

My complete version of the project.:victory_hand:t3:
Appreciate y’all also if you guys comment for better changes.:technologist:t2:

// 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
}

const pAequorFactory= (specimenNum, dna) => {
return {
specimenNum:specimenNum,
dna:dna,
mutate() {
let i=Math.floor(Math.random()*15)
let mutateGene=dna[i]
let newGene=returnRandBase()
if (mutateGene!== newGene) {
dna[i]=newGene;
}
},
compareDNA(otherObj) {
let count=0;
otherObj.dna;
this.dna;
for (i=0; i < this.dna.length; i++) {
if(this.dna[i] === otherObj.dna[i]) {
count++
}
//console.log(count)
}
//console.log(this.dna.length)
//console.log(count)
let percent= (count/(this.dna.length)) * 100
//console.log(percent)
console.log(specimen #1 and specimen #2 have ${percent}% DNA in common)
},
willLikelySurvive() {
let count=0;
for (i=0; i<this.dna.length; i++) {
if(this.dna[i]===β€˜C’ || this.dna[i]===β€˜G’) {
count++
}
}
let percent= (count/(this.dna.length)) * 100
if(percent > 60) {
return true
}
return false
}
}
}

//console.log(mockUpStrand())
let kitty=pAequorFactory(005, [β€˜C’, β€˜A’, β€˜T’, β€˜T’])
let puppy=pAequorFactory(015, [β€˜T’, β€˜A’, β€˜G’, β€˜G’])
let ex1 = pAequorFactory(025, [β€˜A’, β€˜C’, β€˜T’, β€˜G’, β€˜C’, β€˜T’, β€˜C’, β€˜A’, β€˜T’, β€˜C’, β€˜A’, β€˜C’, β€˜T’, β€˜T’, β€˜G’])
let ex2 = pAequorFactory(035, [β€˜C’, β€˜A’, β€˜T’, β€˜T’, β€˜T’, β€˜C’, β€˜G’, β€˜A’, β€˜C’, β€˜G’, β€˜C’, β€˜T’, β€˜A’, β€˜T’, β€˜A’])
let Courtney = pAequorFactory(045, [β€˜C’, β€˜G’, β€˜G’, β€˜G’, β€˜C’, β€˜C’, β€˜G’, β€˜C’, β€˜C’, β€˜G’, β€˜C’, β€˜T’, β€˜A’, β€˜T’, β€˜A’])
kitty.mutate()
//console.log(kitty)
ex1.compareDNA(ex2)
console.log(Courtney.willLikelySurvive())

let studyGroup=
let x=100
for(let i=0; i<30; i++) {
let y=mockUpStrand();
studyGroup.push(pAequorFactory(x,y))
x++
}
console.log(studyGroup)

This is my completed code. It took quite a while and I had to figure out ways to do steps 4, and 5 because they seemed a bit confusing. I really enjoyed this project overall though

Hey, this is my version  ` 1. The project involves simulating the DNA of a fictional organism called *P. aequor*. Each organism has a unique DNA sequence made up of 15 bases, represented by the letters β€˜A’, β€˜T’, β€˜C’, and β€˜G’. The goal is to create and analyze these organisms using a series of functions and methods.

Key Components

  1. Factory Function:
     The `pAequorFactory` function creates an organism with a unique identifier and a DNA sequence. It returns an object with properties and methods to manipulate and analyze the DNA.
  2. DNA Mutation:
    The `.mutate()` method randomly changes one base in the DNA to simulate mutation, ensuring the new base is different from the original.
  3. DNA Comparison:
    The `.compareDNA()` method compares the DNA of two organisms and calculates the percentage of identical bases in the same positions.
  4. Survival Likelihood:
    The `.willLikelySurvive()` method checks if an organism is likely to survive based on having at least 60% of its DNA made up of β€˜C’ or β€˜G’ bases.
  5. Complementary DNA:
    The `.complementStrand()` method generates the complementary DNA strand, where β€˜A’ pairs with β€˜T’ and β€˜C’ pairs with β€˜G’.
  6. Creating Surviving Organisms:
    A function generates 30 organisms that are likely to survive, based on their DNA composition.

 Purpose

This project helps understand genetic concepts like mutation, DNA comparison, and survival likelihood in a simplified and interactive way. It uses programming to simulate biological processes, making it a fun and educational exercise in both biology and coding.`

// 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;
};

//  Returns an object that contains the properties specimenNum and dna
function pAequorFactory(num, arr) {
  return {
    specimenNum: num,
    dna: arr,

    mutate() {
      const dnaBases = ["A", "T", "C", "G"];
      const randomIndex = Math.floor(Math.random() * this.dna.length);
      let newBase = this.dna[randomIndex];

      // Ensure the new base is different from the current base
      while (newBase === this.dna[randomIndex]) {
        newBase = dnaBases[Math.floor(Math.random() * dnaBases.length)];
      }

      // Replace the base at the random index
      this.dna[randomIndex] = newBase;

      return this.dna;
    },

    // Comparing the current pAequorβ€˜s .dna with the passed in pAequorβ€˜s .dna and compute how many bases are identical and in the same locations.
    compareDNA(otherPAequor) {
      let identicalCount = 0;

      for (let i = 0; i < this.dna.length; i++) {
        if (this.dna[i] === otherPAequor.dna[i]) {
          identicalCount++;
        }
      }

      const percentage = (identicalCount / this.dna.length) * 100;
      console.log(
        `specimen #${this.specimenNum} and specimen #${
          otherPAequor.specimenNum
        } have ${percentage.toFixed(2)}% DNA in common.`
      );
    },

    willLikelySurvive() {
      let countCG = 0;

      for (let base of this.dna) {
        if (base === "C" || base === "G") {
          countCG++;
        }
      }

      const percentageCG = (countCG / this.dna.length) * 100;
      return percentageCG >= 60;
    },
  };
}

const createSurvivingPAequor = () => {
  const survivingPAequor = [];
  let specimenNum = 1;

  while (survivingPAequor.length < 30) {
    const newOrganism = pAequorFactory(specimenNum, mockUpStrand());
    if (newOrganism.willLikelySurvive()) {
      survivingPAequor.push(newOrganism);
    }
    specimenNum++;
  }

  return survivingPAequor;
};





My solution:

That was a nice challenge. It did take me much longer than 30 minutes though. I don’t think the time indicator is accurate. Here is my code: MysteriousOrganismChallenge/main.js at main Β· FDeveloperTest/MysteriousOrganismChallenge Β· GitHub