Mysterious Organism task 9: comparing survivors!

P-P-P-Private Prinny reporting for duty dood!

So today, I’ve been working on the Mysterious Organism task so that I can totally hack the Nethernet and declare myself new Ultra Overlord . I m-m-mean, so that I can become a productive member of the netherworld society!

My wonderful (and complicated) code works great. Apart from with task 9…

I have no idea if you can make a method automatically run when you call the object. So, I made a run function that runs the factory and creates an army of minions!

But all the end results are stored in arrays in the run function. When I try to then return the DNA string of the survivors back to my compareDNA method in the factory. it ends up going nowhere.

for (let first = 0; first < survivors.length; first += 1) {
for (let second = 0; second < survivors.length; second +=1) {
if (first !== second && second > first) {
console.log(result.compareDNA(survivors[first].DNA, survivors[second].DNA));

I have discovered that if I move the result = pAerquorFactory variable outside a set of curly braces I can get the compareDNA function working! But… Then all of my minions have the number 1, which while it makes their names easy to remember, it can get confusing.

So, if anyone can help me figure out how to finish this task it would be much appreciated because I can get back to sleeping and other important tasks!

I would also like it know if it is possible to run a method when the object is called.

Also, does anyone know where they keep the answer sheets so that I can take a look at an A+++ score?

I better go now otherwise Master Etna will p-p-p-punish me again gulp.

Thanks dood!

// 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 = (number, strand) => {
  return {
    specimenNum: number,
    dna: strand,
    mutate() {
      const rand = Math.floor(Math.random() * 15);
      let newdna = this.dna;
      let i = this.dna[rand];
      //console.log(this.dna.join(""), rand, i);
      //console.log(i);
      while (i === this.dna[rand]) {
      switch (Math.floor(Math.random() * 4)) {
      case 0 : i = "A";
      break;
      case 1 : i = "C";
      break;
      case 2 : i = "G";
      break;
      case 3 : i = "T";
      break;
      }; //console.log(i);
      }; 
      newdna[rand] = i;
      //console.log(newdna.join(""));
      return newdna.join("");
    },
    compareDNA(base = this.dna.join(""), arr, num1, num2) {
      const sample1 = base;
      const sample2 = arr;
      const storage = [];
      for (let v = 0; v < sample1.length; v++) {
        if (sample1[v] === sample2[v]) {
          storage.push(sample1[v]);
        }; 
      }; 
      //console.log(storage.join(""));
      //console.log((storage.length / 15 * 100).toFixed(0));
      return `The samples provided have ${((storage.length / 15 * 100).toFixed(0))}% of their DNA in common.`;
    }, 
    survival() {
      const chance = this.dna.join("");
      const store = [];
      for (let s = 0; s < chance.length; s +=1) {
        if (chance[s] === "C" || chance[s] === "G") {
          store.push([s]);
        };
      }; if(store.length / 15 * 100 >= 60) {
        return true;
        //`Specimen will likely survive, DNA composition is ${(store.length / 15 * 100).toFixed(0)}% Cytosine and Guanine`;
      } else {
        return false;
        //`Specimen will likely perish DNA composition is only ${(store.length / 15 * 100).toFixed(0)}% Cytosine and Guanine`;
      }
    },
    completeStrand(string = this.dna.join("")) {
     const first = string;
     const second = [];
     for (let t = 0; t < first.length; t+=1) {
       const r = first[t];
       switch (r) {
         case "A" : second.push("T");
         break;
         case "C" : second.push("G");
         break;
         case "G" : second.push("C");
         break;
         case "T" : second.push("A");
         break;
       };
     };
     return second.join("");
     },
  }
};

const run = () => {
  let creatures = [];
  let survivors = [];
  let idPool =[];
  let i = 0;
  while (survivors.length < 30) {
    const result = pAequorFactory(i + 1, mockUpStrand());
    //Check as you go logs (Prinnies are lazy, so checking as you go means less work in the end dood!)
    //console.log(result.compareDNA("CTCACATATTAGGCG"));
    //console.log(result.survival());
    //console.log(result.completeStrand());
    //console.log(`Specimen Number: ${result.specimenNum}\nDNA String: ${result.dna.join("")}\nMutation: ${result.mutate()}`);
    const mutate = result.mutate();
    const specimen = {
      Number: result.specimenNum,
      DNA: result.dna.join(""),
      DNA2: result.completeStrand(),
      Mutation: mutate,
      Mutation2: result.completeStrand(mutate),
    };
    if (result.survival() === true) {
      survivors.push(specimen);
    } else {
    creatures.push(specimen);
    };
    i++
  };
    /*for (let first = 0; first < survivors.length; first += 1) {
    for (let second = 0; second < survivors.length; second +=1) {
      if (first !== second && second > first) {
        console.log(result.compareDNA(survivors[first].DNA, survivors[second].DNA));
        console.log(first, second);
      };
    };
  };*/
  console.log(survivors);
};
run();
2 Likes

Welcome to the forums! Disgaea fan, good taste.

@course3065164934 your subjects doesn’t seem to be declared anywhere. So when you write while (subjects.length < 30) that’s throwing one error preventing your code from running. Have you tried compiling this in a text editor?

@course3065164934 it’s supposed to be survivors? It’s just a little hard to follow what you’re trying to do, if you comment each method a bit more I might be able to spot where it hits a snag.

Thanks for the response! That was a minor error from when I changed the variable name.