Mysterious Organism extra challenge

This is regarding the final step of:

It asks you to

  • Use the .compareDNA() to find the two most related instances of pAequor .

(presumably from the array of 30 objects we created in an earlier step).

So, it suggests you to use the .compareDNA method to achieve this, however the method returns a string. I made my method the following way:

const pAequorFactory = (organismNumber, arr) => {
  return {

    specimenNum: organismNumber,
    dna: arr,

      let similarCounter = 0;
      const DNALength = this.dna.length;
      for (let i = 0; i < DNALength; i++)
        if (this.dna[i] === pAequorObj.dna[i])
      const similarityPercent = similarCounter / DNALength * 100;
      return `Specimen #${this.specimenNum} and specimen #${pAequorObj.specimenNum} have ${similarityPercent.toFixed(2)}% DNA in common.`

My idea for completing this step was to first create the array of 30 objects, then iterate through them and compare each one’s DNA with the other, then .push the two most similar ones to a new bestMatches array. However, since .compareDNA returns a string this makes that a bit tricky. My question is if there is a way to retrieve the value similarityPercent from each comparison while iterating without having to modify the method or what it returns?

The instruction says:

.compareDNA() **does not return anything**, but prints a message that states the percentage of DNA the two objects have in common — use the .specimenNum to identify which pAequor` objects are being compared.

So if you want to strictly follow the instructions, just delete the return statement at the end, make the string log into the console and then add a return statement with just comparison values or whatever you need to complete the bonus task :slight_smile: I just copied the content from compareDNA (I know it’s probably not the best practice, but my comparison solution was 2 lines so it didn’t clutter a whole program) and created another method which returned an array with only necessary values [firstSpecimenNum, secondSpecimenNum, percentage]. This made things easier to later work on finding the most similar pair while keeping track of what the program actually does, that there are no double-checks(like 2 with 4 and later 4 with 2), which pair has which percentage, etc.

1 Like

Interesting, I completely missed the part where it said compareDNA should return nothing.

I just “cheated” then, by making it console.log the string for that step before returning the survivability rate variable.

I’d still be curious to know if the initial way I envisioned it would be possible, though.

I also wonder how you coded it to prevent double checking pairs. The outcome is still the same even with double checking, but would be nice to know.

I went a little off script for this one too and had the .compareDNA() method return the percentage.

But to answer the question…
Hmmm… maybe each specimen can have a similarityPercent property which gets set when compareDNA is ran? Then you could search for the 2 with the highest values retrieved from that property. Not sure if that logic would work… you might run into specimens with an equal percent but do not match because their percent was based off a different comparison. Just thinking out loud - like I said I went a little off script to make it easier. Good luck!

1 Like

Yeah the outcome is the same, but imagine having a database of 30 000 specimen and comparing each and every one. Not having to compare number 1000 with number 1 after already comparing number 1 with number 1000 would save a lot of memory and time. Or a better example: when we reach number 30 000, without double checking, we don’t have to iterate another 29999 times to compare it to every specimen before it since the comparison is already done for every number. Way more efficient. I achieved it with a nested for loop and incrementing counters so that when we for example compare specimen number 4, we don’t don’t perform the comparison on numbers 3, 2, 1, etc. The outcome of comparing 4 different specimen would be something like this: [specimenNum, difSpecimenNum, percentage]

[ [ 1, 2, 40 ],
  [ 1, 3, 6.67 ],
  [ 1, 4, 26.67 ],
  [ 2, 3, 40 ],
  [ 2, 4, 40 ],
  [ 3, 4, 20 ] ]
1 Like

So basically you have it only compare to numbers that are higher than its own, to prevent redundant comparisons, right?

Edit: Yeah just changing the conditional for the different indexes from != to < did the trick, was even simpler to do than I anticipated.

1 Like