Mysterious Organism Challenge Project (JavaScript)

Helper Function to create 30 Test Instances of pAequor

const studyDNA = num => {
  const survivingArr = [];
  let survivingCount = num;
  let testObject = {};
  let specimenNum = 1;

  do {
    testObject = pAequorFactory(specimenNum, mockUpStrand());
    specimenNum += 1;

    if (testObject.willLikelySurvive() === true) {
      survivingArr.push(testObject);
      survivingCount--;
    }
  } while (survivingCount > 0);

  return survivingArr;
};

Here is my code I ended up going with Codecademy/Challenge Project: Mysterious Organism.js at main · ndandan/Codecademy · GitHub

Hi, I know that this post is old but nonetheless I’m going to reply as it might help somebody currently.

It was not clear whether you were talking about the solution file from codecademy or the Node.js installation file. In either case, you need to unzip the file you downloaded first. Use something like 7zip program or any decompression program that is available for Windows 10.

Once the file is unzipped, in the case of the solution code, you can load it up in Visual Code studio, or in the case of Node.js installation file you can then install as per the instructions.

Regards,
Leon Poyau

Morning everyone, here’s my solution. Please have a look and tell me if there some simple refactoring I can do :joy: :sweat_smile: learning bit by bit! Lets get following each other on Github! :man_technologist:

Hi everyone,
Here is my code. Feel free to leave feedback :slight_smile:

1 Like

You can optimize a couple of areas in your solution to be a bit cleaner:

3: let survivingCount = num;
This can probably just be omitted and you can use your passed in num parameter. You really only want to create and assign a new variable with your param’s value if you’re going to mutate it’s value

9: specimenNum += 1;
You could re-write this as specimenNum++. You used similar syntax in your if statement so you want to be consistent

For the overall approach, this works perfectly fine, but you could simplify this a bit by nesting the do…while loop inside of a for loop. That would let you take advantage of that index to save lines where you’re incrementing.

Here’s how I set mine up for reference.

// Question 7 - make a factory for the factory
function superiorStrands(num) {
  const strandsArray = [];

  for (let i = 1; i <= 30; i++) {
    do {
      strandsArray[i] = pAequorFactory(i, mockUpStrand());
    } while (!strandsArray[i].willLikelySurvive());
  }
  
  return strandsArray;
}
1 Like

For lines 64+, you may want to look at making that its’ own function with a parameter you can pass in. This makes it reusable so you can call the function and specify how many strands you want to create.

There are a few bugs I noticed:

Mutate()

  • As a good habit, you want to remove console logs from within your code when you’re done testing. This prevents extraneous outputs being shown to a user. As an alternative, if you want to test values you can create console.logs at the end of your code to check values, and then comment them out as needed.

compareDNA()

  • For line 43, you want to use absolute equality (===) instead of relative equality (==) for your check. While in this exercise it doesn’t hurt anything, it’s another good practice when checking values to only accept exactly what you’re testing for to reduce the likelihood of bugs.

willLikelySurvive()

  • This method always returns the value of true, which is not correct. This method should return true or false depending on how strong your DNA strand is. The rest of your code here is pulling a percentage, but you need to still check if it’s a high enough percentage (over 60%) and then return that to the user.

Question 7 - Return 30 surviving strands

  • Your viableArray is returning 31 results, not 30 - This is due to you initializing on zero, but then also checking for if the value is equal to 30. To correct this, you’ll want to either revise your initialized value you’re looping over for the index (such as making this 1), or change your stopping point in your conditional check (such as making this < 30 instead of <=30).
  • The returned array does not check and return 30 surviving DNA strands. This is due to your bug in willLikelySurvive() as it’s always returning true, so you’re adding strands that shouldn’t be there.
1 Like

Thanks for reviewing my code, totally agree with all your points mentioned. Great advices!

Your code looks really nice for this kind of function. But in the for loop there is

for (let i = 1; i <= 30; i++) {

shouldn´t it call num there, instead of 30?

for (let i = 1; i <= num; i++) {
1 Like

That’s a good catch, thanks! I totally missed that.

1 Like

Thanks for looking over my code! It’s amazing how much you miss after staring it for a while :sweat_smile:

  • Removed the unnecessary logging
  • Fixed the counter
  • Fixed the willLikelySurvive function & other minor bugs.

Thanks so much for helping out! :slight_smile:

Hi

Here I leave a link to my solution: link

Best
Ilan

Greetings,

Here is my challenge code for the mysterious organism. For this instance, I replace that species with pokemon for more fun. I couldn’t do the bonus because the instruction was unclear.

The Code

Cheers,
FS

Thanks for your feedback. I’ll change it.

Hello people;)

If anyone can explain in regards to the last task(extension), its said that we need to use function compareDna, where similarities are in persentages, though as far as i understand it should be new Arr that has been created for each and every DNA(following explanation of what Complimantary Strand is), or should we find the most similar one from survivedSpecies arr, from last exersice? thank you and good luck to all<3

I read it as - find the two most closely related (highest % DNA in common) P.Aequor organisms in your array of 30.

Hello I’m 35% through the Frond End Developer path. This is my solution for the mysterious organism challenge Mysterious-Creature/main.js at main · fquinn454/Mysterious-Creature · GitHub
All feedback gratefully received.
Thanks,
Fiona

here is my completed code for this project

Hey guys, I really appreciate how fun this project was :slight_smile: I stored my solution here on my github (fell free to add me): JavaScript-challenge-projects/mysteriousOrganism.js at main · eliseflaneuse/JavaScript-challenge-projects · GitHub

But I leave it here too:

// 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; }; /* Factory Function with three methods .mutate() - simulate P. aequor‘s high rate of mutation (change in its DNA). .compareDNA() - compare two strings of dna .willLikelySurvive() - more than 60% of Cytosine, and Guanine present. */ function pAequorFactory(num, arr) { return { specimenNum: num, dna: arr, mutate() { let index = Math.floor(Math.random() * 15); let newBase = returnRandBase(); if (this.dna[index] !== newBase) { return this.dna.splice(index, 1, newBase) } else { newBase = returnRandBase(); } }, compareDNA(obj) { let identical = 0; let percentage = 0; for (let i = 0; i < this.dna.length; i++) { if (this.dna[i] === obj.dna[i]) { identical++, percentage = identical / 16 * 100, console.log('specimen #1 and specimen #2 have ' + percentage + '% DNA in common.') } } }, willLikelySurvive() { let goodGenes = 0; let percentage = 0; for (let i = 0; i < this.dna.length; i++) { if (this.dna[i] === 'C' || this.dna[i] === 'G') { goodGenes++; percentage = goodGenes / 16 * 100 } }; if (percentage >= 60) { return true } else { return false } } } }; //tests: let pAequor1 = pAequorFactory(1, mockUpStrand()); let pAequor2 = pAequorFactory(2, mockUpStrand()); console.log(pAequor1); console.log(pAequor2); pAequor2.compareDNA(pAequor1); pAequor2.mutate() console.log(pAequor2); console.log(pAequor1.willLikelySurvive()) // End of tests // Funtion that creates 30 instances of pAequor that can survive in their natural environment. Stored in an array. const specimensForStudy = []; function findingBest () { console.log(specimensForStudy); for (let i = 0; specimensForStudy.length < 30; i++) { let pAequor = pAequorFactory(i, mockUpStrand()); if (pAequor.willLikelySurvive() === true && specimensForStudy.length < 30) { specimensForStudy.push(pAequor) } } }; findingBest(); console.log(specimensForStudy.length); console.log(specimensForStudy);

Hi strangerFriends,

This one seemed easier to me than the credit care project. Still took me a few days and a lot of learning. I hope it continues to click for me…

pAequor/script.js at main · kelfromdablock/pAequor (github.com)

Cheers.