Mysterious Organism Challenge Project (JavaScript)

Another tough one.

Here’s my solution: Codecademy export · GitHub

Here is my solution. The one issue I had was I wasn’t sure which strands the compare function was supposed to compare. I had it compare to a random new strand since the instructions weren’t clear.

Hi everyone,
my solution with the extra tasks. I would be grateful for a code review!

Hey! Here’s my take:

//Generate a random number between 0 and 3 const randGenerator = () => { return Math.floor(Math.random() * 4); } // Returns a random DNA base const returnRandBase = () => { const dnaBases = ['A', 'T', 'C', 'G']; return dnaBases[randGenerator()]; }; // 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; }; //Object factory const pAequorFactory = (orgNum,inputDna) => { return{ speciesNum: orgNum, dna: inputDna, mutate: function() { //generate random number between 0 and 14 let el = Math.floor(Math.random() * 14); console.log('dna[el] is '+this.dna[el]) let equal = true; //while dna[el] base is equal to randomBase while(equal === true){ let randBase = returnRandBase(); if(this.dna[el] !== randBase){ this.dna[el] = randBase; console.log('new DNA is '+this.dna[el]) //break loop equal = false; } else test = true; } }, compareDNA: function(obj) { let counter = 0; //compare both arrays for(i = 0; i<15; i++){ if(obj.dna[i] === this.dna[i]){ counter++; } } //convert to % let percentage = Math.round(counter*100/15); console.log('specimen #1 and specimen #2 have '+percentage+'% DNA in common.') }, willLikelySurvive: function(obj){ let counter = 0; //count C and G instances for(i = 0; i<15; i++){ if(obj.dna[i] === 'C' || obj.dna[i] === 'G'){ counter++; } } //convert to % let percentCounter = Math.round(counter*100/15); if(percentCounter >= 60){ console.log('More than 60% C/G') return true; } else return false; } } } let pAequorArray = []; //create array with 30 objects for (i=0; i<30; i++){ pAequorArray.push(pAequorFactory(i, mockUpStrand())); } console.log(pAequorArray)

Fun stuff!

// 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 = (aNumber, dnaArr)=>{ return { specimenNum: aNumber, dna: dnaArr, mutate() { let dnaBases = ['A','T','C','G']; const id = Math.floor(Math.random()*15) let base = this.dna[id] dnaBases.splice(dnaBases.indexOf(base),1) return this.dna.splice(id, 1,dnaBases[Math.floor(Math.random()*3)]) }, compareDNA(pAequor){ const thisLength = this.dna.length; const otherLength = pAequor.length; let countMatches = 0; console.log(thisLength); for (let i = 0; i < thisLength; i++){ console.log(`${i} : ${this.dna[i]} and ${pAequor[i]}`); if (this.dna[i] === pAequor[i]) { console.log(`${this.dna[i]} and ${pAequor[i]}`) countMatches = countMatches + 1; } } const percentage = Math.floor(countMatches/thisLength* 100); return `specimen #1 and specimen #2 have ${percentage}% DNA in common`; }, willLikelySurvive(){ let counter = 0; for(let i = 0; i < 15; i++){ if(this.dna[i] === 'C' || this.dna[i] === 'G'){ counter ++; } } // console.log(counter) if(Math.floor(counter / 15 *100) >= 60){ return true; } else { return false;} } } }; const sample1 = pAequorFactory(1,mockUpStrand()); const sample2 = pAequorFactory(2,mockUpStrand()); console.log(sample1); sample1.mutate(); console.log(sample1); console.log(sample2); console.log(sample1.compareDNA(sample2.dna)); console.log(sample1.willLikelySurvive()); let i = 1; let dnaArr = []; do{ let sample = pAequorFactory(i, mockUpStrand()); if(sample.willLikelySurvive() === true){ dnaArr.push(sample); i++; } } while(i <31); console.log(dnaArr);

Not very precise and short but I guess code quality will surely improve gradually but for now as long as it works, right? :slight_smile:

Can someone please explain to me whate the line =

   if (arr[idx] === otherOrg.dna[idx]) 

= in the solution means.

I am confused

That seems to be used to check whether array (an array that holds the letters representing the dna) and the .dna array for otherOrg have the same letter at the each index (and that index is idx ).

Sorry I am still confused

Is the .dna like ‘.this’?

What’s does “ the .dna array for otherOrg” mean?

Each of the objects made by the pAequorFactory function have the properties specimenNum, dna, and so on.
otherOrg is assumed to be another object created by using this function;
thus otherOrg should have a dna property (and it could be accessed by doing otherOrg.dna )

“You’ll have to add a new method (.compareDNA() ) to the returned object of the factory function.”

This is the instructions in step 5. Therefore otherOrg is a parameter to the .compareDNA() method of the factory function. How can otherOrg be an object?

The only thing I can think of is that - arr[idx] === otherOrg.dna[idx] - means otherOrg.‘A’ for example or whichever letter comes from the .dna[idx]. Could be otherOrg.‘C’ or otherOrg.‘T’ or otherOrg.‘G’

objects can be arguments/parameters for functions (and methods).
compareDNA is a method for an object that is returned by the pAequorFactory function. (pAequorFactory returns an object; and the object that is returned has a compareDNA method.)
compareDNA is not a method for pAequorFactory itself.

1 Like

heres my attempt at it. hopefully its all correct. tested as i went through it.let me know what you guys think!

ok think i got it thanks so much for your help!

so if otherOrg is another object, does it have the compareDNA method with the parameter otherOrg?

Yes, if otherOrg is an object created by the pAequorFactory, it will have a compareDNA method.

1 Like

oh but nothing will be passed into it so its not an endless loop