Javascript - return in an if... statement not working - Help please

Hello,
I am new to this forum as a learner. I need help to understand why the return true / false is not woking in the if…statement in my code below please. This is part of the Javascript mysterious organism project. I will appreciate if someone could explain to me what I am not doing right and the correct way to go about it please. Many thanks in advance.


willLikelySurvive () {
  let C = pAequorFactory(3, mockUpStrand());
  c = C.dna;
 // console.log(c);

 let cCount = 0;
 let gCount = 0;

 for(let x=0; x<c.length; x++){
  if(c[x] === 'C'){
    cCount++;
   // Printing out locations where dna bases equal 'C' or 'G'.
  //console.log( c[x] + ' = C at location '+ x );
   } else 
   if(c[x] === 'G'){
       gCount++;
 // console.log( c[x] + ' = G at location '+ x);
   }

 }
 // Printing out sum count of bases equal 'C' or 'G'.
 //console.log('cCount = '+ cCount + ', gCount = '+ gCount);
 let cPcent = (((cCount/c.length) * 100).toFixed(1));
// Printing out the % of 'C' in the dna.
 // console.log('cPcent = '+ cPcent + '%');

  let gPcent = (((gCount/c.length) * 100).toFixed(1));
 
 //Printing out the % of 'G' in the dna.
// console.log('gPcent = '+ gPcent + '%');

 if(cPcent >= 60 || gPcent >= 60) {
   return true; // WHY IS 'true' NOT RETURNED, DESPITE THE if-condition IS MET?
 } else {
   return false;     // WHY IS 'false' NOT RETURNED, DESPITE THE if-condition IS NOT MET?
 }
}   

Hi,
can you provide a link to the lesson, please?
Is it only the if statement at the bottom of your code that isn’t working?
I’m particularly wondering about these parts:

You’re calling the factory function from within its method. Does this ‘C.dna’ return anything valid?

I don’t see where c is initialized, does the console log anything valid?

Hi Mirja,

Here is the link:
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-challenge-project-mysterious-organism/projects/mysterious-organism

Yes, it is the only if…statement at the bottom which is not working. However, if I say return console.log("false"); or return console.log("true"); it prints out false/ true to the console. But will not return true / false by itself!

Thanks for the link. I’m still confused about the scoping, what about this:

How do you call willLikelySurvive() and where is it located? It looks like a method of an object (due to no const or function keyword) – so can you provide all of your code?

Hi,
Below is the whole code:

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

//console.log(returnRandBase());
//console.log(mockUpStrand());


function pAequorFactory(number, array) {
  return {
    specimenNum: number,
    dna: array,
    mutate() {
      let randomNum = Math.floor(Math.random() * 15);
      let newDNA = returnRandBase();

      while(newDNA === array[randomNum]) {
        newDNA = returnRandBase();
      }; 
      array[randomNum] = newDNA;      
      return array;
    },



compareDNA (obj) {
 let B = pAequorFactory(2, mockUpStrand());
 // current pAequor‘s dna
  let b = B.dna;
  console.log(b);
  //console.log('B.specimenNum = ' + B.specimenNum);

// passed-in pAequor‘s dna (i.e. - argument passed in).
console.log(obj);
let count = 0;
 
for(let x=0; x< b.length; x++){
  for(let x=0; x<obj.length; x++){
  }  


// Bases that are identical and in the same locations.
  if(b[x] === obj[x]) {
  count++;
 //   console.log('count = ' + count +'. '+ b[x] +' = ' + obj[x] + ', at index: ' + x );
  }
}
console.log('specimen #1 and specimen #2 have '+ ((count / 15) * 100).toFixed(0) + '% DNA in common.');     
    },



willLikelySurvive () {
  let C = pAequorFactory(3, mockUpStrand());
  c = C.dna;
  console.log('\'console.log(c);\' prints out an array of the dna strand with 15 bases as shown below:' + '\n')
  console.log(c);

 let cCount = 0;
 let gCount = 0;

 for(let x=0; x<c.length; x++){
  if(c[x] === 'C'){
    cCount++;
   // Printing out locations where dna bases equal 'C' or 'G'.
  //console.log( c[x] + ' = C at location '+ x );
   } else 
   if(c[x] === 'G'){
       gCount++;
 // console.log( c[x] + ' = G at location '+ x);
   }

 }
 // Printing out sum count of bases equal 'C' or 'G'.
 //console.log('cCount = '+ cCount + ', gCount = '+ gCount);
 let cPcent = (((cCount/c.length) * 100).toFixed(1));
// Printing out the % of 'C' in the dna.
  console.log('cPcent = '+ cPcent + '%');

  let gPcent = (((gCount/c.length) * 100).toFixed(1));
 
 //Printing out the % of 'G' in the dna.
 console.log('gPcent = '+ gPcent + '%');

 if(cPcent >= 40 || gPcent >= 40) {
   return console.log('true'); // WHY IS 'true' NOT RETURNED, DESPITE THE if-condition IS MET?
 } else {
   return console.log('false');     // WHY IS 'false' NOT RETURNED, DESPITE THE if-condition IS NOT MET?
 }
}    
  }
 }







//console.log(pAequorFactory(1, mockUpStrand()));  
          


  let newObj = pAequorFactory(1, mockUpStrand);
 
   const obj = newObj.dna();
  
  // The code below runs the compareDNA method.
  // newObj.compareDNA(obj);

 // The code below runs the willLikelySurvive method.
 newObj.willLikelySurvive();
    


Hi,

The console.log(c); prints out an instance of the array dna with the 15 bases.

See screenshot below:

![image|690x388](upload://mOqeK4iYq52wlGV2BzampISnPPv.png)

'console.log(c);' prints out an array of the dna strand with 15 bases as shown below:

[ 'A', 'G', 'A', 'G', 'C', 'G', 'A', 'A', 'C', 'C', 'G', 'G', 'A', 'G', 'T' ]

A few things to observe:

  1. willLikelySurvive () is a method of pAequorFactory. So it should be called on an instance of pAequorFactory. Instead, you’re calling it within its own method.
  1. What you’re returning is a console – not a boolean. So what your calling function gets in return is ‘undefined’. Just return ‘true’ or ‘false’. You can then call your method and log the result of that function call.

Hi,
Many thanks for your observations.

The return console.log('true'); is just an attempt to find a work around, the main challenge is that return true; and the return false; within the if…statement is not working. I wonder why and how can I make it work. Suggestions welcome please.

This is still a valid solution. If you follow that, you will see the result.
So far you are calling the method without logging its result. So you cannot see what is returned. Just what you log within the method’s body.

Many thanks for your help. Much appreciated.

1 Like