// 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, array15) => {
let specimenNum = number;
let dna = array15;
return {specimenNum,
dna,
mutate(){
let random = this.dna;
let i = Math.floor(Math.random()*15);
let base = random[i]
if (base === 'A'){
return (random[i] = 'T', console.log(random));
} else if(base === 'T'){
return (random[i] = 'A', console.log(random));
} else if (base === 'C') {
return (random[i] = 'G', console.log(random));
} else {return (random[i] = 'C', console.log(random));}
},
compareDNA(object) {
let obj1 = this.dna;
let obj2 = object;
let total = 0;
for (i =0; i < obj1.length; i++){
for(j=0; j < obj2.length; j++){
if(i === j && obj1[i] === obj2[j]){
total = total + 6.67;
}
}
}
return (obj1, obj2, total, console.log(total));
},
willLikelySurvive() {
let survivors = this.dna;
let total = 0;
for (i=0; i<survivors.length; i++){
if (survivors[i] === 'C' || survivors[i] === 'G') { total = total + 6.67
}
}
if (total >= 60) {
return true;
} else {return false;
}
}
}
};
const survivors = () => {
let survivorsArray = [];
let dna = pAequorFactory.dna;
let willSurvive = pAequorFactory.willLikelySurvive;
if (survivorsArray.length <= 30 && willSurvive === true){
survivorArray.push(pAequorFactory.dna)
}
return survivorsArray;
}
//let test = pAequorFactory(1, mockUpStrand());
//console.log(test.specimenNum);
//console.log(test.dna);
//test.willLikelySurvive();
console.log(survivors());
The above is my code. It works up until the const survivors function; I can’t figure out how to get methods on my object into my function. The array keeps coming back blank.
You’ll need specimens to test. The factory function itself is not a pAequor object. You’ll need to create the pAequor objects to invoke the .willLikelySurvive() method on, and then push them into your survivorsArray.
Moving the function into the factory function is unnecessary. The function will need to generate the objects, invoke the .willLikelySurvive() method on them, and then push them into the array if they pass. This process will need to repeat until the array has 30 pAequor instances that are likely to survive. When we want to fill an array with a specified number of elements as we do here, a while loop is a good way to go. Something with a structure similar to the following would work.
const myFunction = () => {
myArray = [];
//will continue creating and testing objects until the array has 10 elements
while(myArray.length < 10) {
//create object
//see if object meets requirements and add it the the array if it does
if(object.meetsRequirements()) {
myArray.push(object);
}
}
return myArray;
}
The while loop takes care of that. You may have to create 100’s of instances to get 30 that pass willLikelySurvive(). Recall how you created your test object in the other topic earlier using the mockUpStrand() function. That’s how you’ll want to do it.
This: let test = pAequorFactory(1, mockUpStrand());
ah and it’s better to use mockUpStrand since it’ll loop through different arrays until it gets to 30 where if you put a value, it’ll just return that value 30 times.
We seldom, if ever, need to return true or return false if we return an expression that will evaluate to true or false the return value is the expression’s evaluated value like the example above.
The following two examples return the same thing: