Mysterious Organism, Task 4

Greetings, oh wise ones! So, I’m working on the Mysterious Organism project and I was hoping if anyone could point out where am I failing here in task 4:

// 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
}
// my code

const pAequorFactory=(number, array)=>{
  return {
  specimenNum: number,
  dna: array,
  mutate(){
    const randomIndex = Math.floor(Math.random() * this.dna.length);//select a random element in the DNA array
    
    let newBase = returnRandBase();
    console.log(newBase);// Log 1: random generated DNA base
    let randomBase = this.dna[randomIndex];//randomly selected base
    console.log(randomBase);//Log 2: random element selected at the DNA array
      console.log(testDna.indexOf(randomBase));//Log 3: randomly selected base at object's DNA index position
    while(this.dna[randomIndex]===newBase){
      newBase = returnRandBase();    
    }
    this.dna.splice(this.dna[randomIndex],1,newBase);
    return this.dna;
  }
}
}

const testDna = [ 'T', 'T', 'C', 'A', 'T', 'C', 'C', 'T', 'C', 'A', 'T', 'T', 'A', 'T', 'T' ];

const testMethod = pAequorFactory(1, testDna);
console.log(testMethod.mutate());

I’ve generated a random array at “const testDna” to test the method and, as it turns out, the method is not altering the original array:

Any ideas?
Thank you so much

1 Like

I am by no means an expert, just another student like you, but I tried your code and I think it works perfectly. Maybe try copying the code, resetting the exercise, and pasting the code back again?

1 Like

Although, after having a second look, I think the idea was to change a random index, not an index with a certain letter, so maybe instead of

...splice(this.dna[randomIndex], 1, newBase)

where this.dna[randomIndex] is the value of that element (the letter/base), you could do:

...splice(randomIndex, 1, newBase)

This way would ensure you change about any random index in the array.

Although not 100% sure if that was your goal?

1 Like

Hey mate, thank you for your reply!
It’s weird, it seems like my while loop is not doing its job maybe?
So I’ve changed the splice method like you suggested, but it picks the same letter from both bases making it impossible to swap it…

// 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
}
// my code

const pAequorFactory=(number, array)=>{
  return {
  specimenNum: number,
  dna: array,
  mutate(){
    const randomIndex = Math.floor(Math.random() * this.dna.length);//select a random element in the DNA array
    
    let newBase = returnRandBase();
    console.log(`Log 1: random generated DNA base: ${newBase}`);// Log 1: random generated DNA base
    let randomBase = this.dna[randomIndex];//randomly selected base
    console.log(`Log 2: random element selected at the DNA array: ${randomBase}`);//Log 2: random element selected at the DNA array
      console.log(`Log 3: randomly selected base at object's DNA index position: ${testDna.indexOf(randomBase)}`);//Log 3: randomly selected base at object's DNA index position
    while(this.dna[randomIndex]===newBase){
      newBase = returnRandBase();    
    }
    this.dna.splice(randomIndex,1,newBase);
    return this.dna;
  },

}
}
//task 4
const testDna = [ 'T', 'T', 'C', 'A', 'T', 'C', 'C', 'T', 'C', 'A', 'T', 'T', 'A', 'T', 'T' ];

const testMethod = pAequorFactory(1, testDna);
console.log(`Original array: [ 'T', 'T', 'C', 'A', 'T', 'C', 'C', 'T', 'C', 'A', 'T', 'T', 'A', 'T', 'T' ], Array mutated to:${testMethod.mutate()}`);

So, in this particular instance, it picked “A” from the possible 4 bases, and then picked another “A” at index 3 from the original array…
organism mutation

1 Like

Cool! So it changed a random index (in this case the ‘A’ base at index 9 of orig array became a C, I think, right?).

Wait, isn’t that what you want and what the exercise is asking for?

What were you expecting and what was the actual outcome? Or in other words, how do expectations/outcome differ?

1 Like

If you really want to have a DNA letter different than a given letter, you can do that with a function.
(You could do it with a function similar to returnRandBase, but more complex).

I did that using an extra variable - a counter (which is j in the code below), which helps to indicate which DNA letter to select. (Select the one where randChoice matches j, instead of using the index.)

Notice the loop skips doing anything where currentBase matches the letter in dnaBases by having an if-statement that does continue and there’s an else statement that handles everything else.

// Returns a random DNA base different than the given base
const returnRandOtherBase = (currentBase) => {
  const dnaBases = ['A', 'T', 'C', 'G'];
  let randChoice = Math.floor(Math.random() * 3);
  let otherBase;
  let j = 0;
  for (var c of dnaBases) {
        if (c == currentBase) {
            continue;
        }
        else {
          if (j == randChoice) {
            otherBase = c;
          }
          j++;
        }
  }
  return otherBase;
}

Note that I did not actually do this as a separate function in my project, but there’s nothing wrong with doing so.

1 Like

Wow, you are absolutely right. It changed but not on the index I was looking at, that’s why I thought it wansn’t changing… I’ve logged on the console the random generated base at Log 1, and it logs an A (should be a C). Log 2 was supposed to log the letter selected at the original array which is an A at index 9 but instead, logged the A at index 3… Well, my brain feels like a scrambled rubik’s cube already and it’s not even 10 in the morning, I really have to sit down in front of this to figure out what I’ve done, but you’re right!

1 Like

Cheers mate, thank you so much for your reply! It turns out my code is doing what it was supposed to, is just that I was looking at the wrong index basically. I really like how you solved your problem though, very interesting. It gave me a new perspective, very cool!

Just a follow up on my problem, I found where I messed up. My console.log’s were placed outside of the while loop, that’s why they were throwing different values making me look at the wrong indexes ahah

1 Like

Yes, you were just displaying the wrong index in the code before …

console.log(`Log 3: randomly selected base at object's DNA index position: ${testDna.indexOf(randomBase)}`);

here,
testDna.indexOf(randomBase)
should have been
randomIndex

If your randomBase was C for example, indexOf would find the first C in the array, which might not be at the index where you’re actually changing the value.

1 Like