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.
Morning everyone, here’s my solution. Please have a look and tell me if there some simple refactoring I can do learning bit by bit! Lets get following each other on Github!
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;
}
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.
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.
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.
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
// 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);