Mysterious Organism exercise n.4 just one step to get it right! pls have a look

Hello there, I’m having troubles understanding how to get this function work 100%

Is now working but,

the if/else statement should check wether the actual base and the randomly generated one are different;

if they’re same should retry and give one that is different.
But it doesn’t, even if they’re same it still returns it! :scream:

I guess there something wrong in this phase:

else {
      false
}

Anyone can have a look? thanks a ton

Ff u havent encountered this exercise here’s the link:
https://www.codecademy.com/practice/projects/mysterious-organism

// 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 = (specimenNum, dna) => {
  return {
  specimenNum: specimenNum,
  dna: dna,
    
  mutate(dna) {
    //Chooses randomly the index for the base to be changed
    const dnaIndex = Math.floor(Math.random() * this.dna.length);
    
    //Gets the base at the randomly generated index
    let baseAtThatIndex = this.dna[dnaIndex];
    
    //Calls a randomly generated base
    const randBase = returnRandBase();
    
    //"Should" check wether the actual base and the randomly generated one are different
    if (baseAtThatIndex != randBase) {
       this.dna[dnaIndex] = randBase;
    } else {
      false
    }
 
    return dna
    
    }
  }
}

const unoDna = ["A","C","T","G"]
const uno = pAequorFactory(1, unoDna)

console.log(uno.mutate(unoDna))

Yeah. When false executes what do you suppose happens?

That aside, even if you had code inside the else block that generated a new random base, what would you do if it again was the same as the one you’re trying to replace? Change else to else if and add another else? How many else ifelse's would you need to make absolutely sure that you didn’t get the same value that you started with?

There is a loop especially suited for when we need to repeat a series of steps while a specific condition exists.

That would be one option. I took a different route when I did this project. After randomly choosing the index for the base to be changed, I pass the value at that index to my returnRandBase() function, and choose the new random base from a list with the one I want to replace filtered out. That way it cannot be the same. Here’s my returnRandBase() function.

const returnRandBase = (x = null) => { //x is an optional parameter
  const dnaBases = ['A', 'T', 'C', 'G']
  dnaBasesSelect = dnaBases.filter(e => e != x)
  return dnaBasesSelect[Math.floor(Math.random() * dnaBasesSelect.length)] 
}

When calling the function from my mockUpStrand() function, I don’t supply the optional argument, so the whole array ['A', 'T', 'C', 'G'] is used to generate a new specimen.

Hey dude, I just didn’t know how else formulate it so I thought “false” might just block the function and restart it until it gives me the right result

is there a keyword to do that?

or how should I formulate it with a while loop? coz I tried and it just happened to be a infinite loop then

ahhhh :scream:

If you use the word “repeat” then you’d want a loop, and you mentioned a requirement for stopping, you’d put that in the condition for the loop.

false is a value it’s not going to do very much on its own, and if you are to expect something it better be based on something it has promised it’ll do (otherwise it’s just made up)

then you would look at the thing that controls whether there’s a next loop and adapt it so that it stops when you consider the thing to be done

Hey dude trying and trying I came up with this, seems to work, what do you thing?

// 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 = (specimenNum, dna) => {
return {
specimenNum: specimenNum,
dna: dna,

mutate(dna) {
//Chooses randomly the index for the base to be changed
const dnaIndex = Math.floor(Math.random() * this.dna.length);

//Gets the base at the randomly generated index
let baseAtThatIndex = this.dna[dnaIndex];

//Calls a randomly generated base
const randBase = returnRandBase();

//"Should" Checks wether the actual base and the randomly generated one are different

let continueLoop = true
while(continueLoop === true){
if (baseAtThatIndex != randBase) {
this.dna[dnaIndex] = randBase;
continueLoop = false;

} else {
  continueLoop = true;
  }
}

return dna

}

}
}
const unoDna = [“A”,“C”,“T”,“G”]
const uno = pAequorFactory(1, unoDna)

console.log(uno.mutate(unoDna))

What about the code I just posted?

idk it won’t run (try yourself by copying from your post) (also have you looked at it?)
formatting buttons in the editor or some pastebin, for example paste.gg

If you’ll take a peek at this topic: How do I format code in my posts?, and then edit your post, so we can more easily read/run your code, that would be very helpful.

Opss !

// 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 = (specimenNum, dna) => {
  return {
  specimenNum: specimenNum,
  dna: dna,
    
  mutate(dna) {
    //Chooses randomly the index for the base to be changed
    const dnaIndex = Math.floor(Math.random() * this.dna.length);
    
    //Gets the base at the randomly generated index
    let baseAtThatIndex = this.dna[dnaIndex];
    
    //Calls a randomly generated base
    const randBase = returnRandBase();
    
    //"Should" Checks wether the actual base and the randomly generated one are different
    let continueLoop = true
    while(continueLoop === true){
      if (baseAtThatIndex != randBase) {
       this.dna[dnaIndex] = randBase;
         continueLoop = false;
    } else {
      continueLoop = true;
      }
    }
    
    return dna
    
    }
  }
}
const unoDna = ["A","C","T","G"]
const uno = pAequorFactory(1, unoDna)

console.log(uno.mutate(unoDna))

1 Like

A few things to consider:

  • What happens when the loop repeats? (Aren’t you just comparing the same 2 values over and over?)
  • How could you use the condition in your if statement as the condition for your while loop?

Hey, thanks for getting in touch !

  • It’s supposed to repeat only if the condition is not met; stop and give the result if the condition is met

  • I tried to use the while loop without if/else but I couldn’t find a suitable solution and I ended up with infinite loops !

Is the last code I posted alright? if not what could I improve?

Conceptually there’s only one condition, but in your code there are two conditions.
The condition in a while-statement determines whether or not there should be a next iteration, isn’t that exactly what you’re trying to control? So that is where that condition should go.

You have all of this:

    let continueLoop = true
    while(continueLoop === true){
      if (baseAtThatIndex != randBase) {
       this.dna[dnaIndex] = randBase;
         continueLoop = false;
    } else {
      continueLoop = true;
      }
    }

But out of that, only one line is the action repeated:

this.dna[dnaIndex] = randBase;

And there’s a single condition for whether this should be repeated, so surely it should instead look like so:

while (some condition) {
  this.dna[dnaIndex] = randBase;
}

And if that produces an infinite loop then that is a problem with the condition you wrote, so you would reconsider the condition, not the whole structure of it.