JS Syntax II <Credit Card Checker> & <Mysterious organism>

Hi y’all… I’ve been spending the past 2 days for these 2 projects, yet I can’t complete it without a feeling that something’s off…

  1. For Credit Card Checker - I wanted an extra function that prints out results from all the functions, so I named it checkerTester, but it doesn’t seem to work…
    Here is the link: https://gist.github.com/hypyeon/26c46f5d51df291839ed895d7a432673

  2. For Mysterious Organism, I wrote down my concern at the end of the page.
    https://gist.github.com/hypyeon/18267a5f575db86ae963433ec70555ee

Much appreciated if anyone could help me out. :smiley:

Hey @mega5149737189

So here’s for the Mysterious Organism:

const create30 = () => {
  let pAequorArray = [];
  let i = 1;
  const newDNA = pAequorFactory(i, mockUpStrand());
  const surv = newDNA.willLikelySurvive();
  if (surv === true) {
    i += 1
    do {
      pAequorArray.push(newDNA);
    } while (pAequorArray.length < 30);
  }
  return pAequorArray
};

In the code above what’s interesting is that you’re only checking if the first organism is likely to survive if it is then you’re pushing 30 times the same organism into the array.

What you want to do first is this (in an order)

  1. Create an array - this is what you already have
  2. Create a while loop that loops until pAequorArray.length < 30
  3. Within the loop you want to create a new organism and see if the organism is likely to survive.
  4. If it is push it into the array if not continue
  5. When everything is done, return the array outside of the loop

Everything that I mentioned is already there, you just need to reorganize it better. Also I wouldn’t be using const on newDNA or surv

If you have any issues let me know, I can walk you through the code and why it works the way it works.

This will only solve part of the problem tho. But it’s important to solve this to solve the rest.

Thank you so much for your help!

What I was going for with this :

if (surv === true) {
    i += 1
    do {
      pAequorArray.push(newDNA);
    } while (pAequorArray.length < 30);

is that I wanted to get 30 new DNAs that are already tested true in survival function.

I had tried with the order you have given, it does work, but the number of values of newDNA array are not 30. The randomly selected DNA, after the survival function, are random number of values, certainly less than 30.

How do I proceed from here? :smiley:

I gotcha, with your solution, you’re testing only one DNA true in survival. You want to test every single one. :slight_smile:

Here’s mine:

const create30 = () => {
  let i = 1;
  let pAequorArray = [];
  while (pAequorArray.length < 30) {
    let newDNA = pAequorFactory(i, mockUpStrand());
    let surv = newDNA.willLikelySurvive();
    if (surv === true){
      i++;
      pAequorArray.push(newDNA);
    }
  }
  return pAequorArray
};

This code it works for me. You cannot have less than 30 if you tell it to loop while it is less than 30.

Let me know if you have any questions particularly about this step. It’s very important to understand this.

1 Like