Conditional with boolean did not evaluate correct even though it was

Tell us what’s happening:
Why would if(true)
{return something}
else{
return somethingElse
}

If there are only 2 values for boolean objects, then why was that code not accepted?

Your code so far


// Example
function ourTrueOrFalse(isItTrue) {
  if (isItTrue) { 
    return "Yes, it's true";
  }
  return "No, it's false";
}

// Setup
function trueOrFalse(wasThatTrue) {

  // Only change code below this line.
  if(wasThatTrue)
  {
      return "Yes, that was true";
  }
      return "No, that was false";

  
  
  // Only change code above this line.

}

// Change this value to test
trueOrFalse(false);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-conditional-logic-with-if-statements

Looks like it should work to me.

Try using the Google Chrome browser. The testing software is only guaranteed to work in Chrome.

I don’t really understand your question so I’ll just post something that might help. (holy crap this site does not keep the same indentation I had, I didn’t indent that else or return etc)

<!DOCTYPE html> 
<title>unnamed</title>
<meta charset="utf-8"/>
<canvas id="canvas" width="700" height="550" oncontextmenu="return false"></canvas>

<script>
// Example
function noelse(truth,n){
  if (truth) { 
    n++
  }
  n++ //this line executes every time
  return n //any return statement breaks out of a function
  console.log('hi') // this won't print because above return broke out of function
}

function withelse(truth,n){
  if (truth) { 
    n++
  }
  else{
     n++ //only does this line as catch all if above if is false. use else if(condition) to specify one condition instead of catch all false cases.
  }
  return n
}


// Change this value to test
//press f12 in browser to see console output
//can use 1/0 for false/true
n=6
console.log(noelse(true,n)) //8   //this one added twice cuz there was no else, so line 12 executes always
console.log(noelse(false,n)) //7
console.log(withelse(true,n)) //7
console.log(withelse(false,n)) //7
</script>

Your return phrases are not exactly as asked in the challenge. They must match exactly word for word

1 Like

So, my return phrases were exactly as they asked. The initial question was cut off. My question was basically why cant I just use an if else. If you go to the challenge, it wont let you use an if else. I copied and pasted the return strings from the directions. Also the first test (if) passed and the else test did not pass.

OK - your phrases are correct. Sorry, I re-read that challenge from my phone and I got that wrong.

On my computer I am able to pass with the code you provided and many variations of that if statement.

// these all pass that challenge for me using Chrome browswer

function trueOrFalse(wasThatTrue) {
   if(wasThatTrue){
      return "Yes, that was true";
  }
      return "No, that was false";
}

function trueOrFalse(wasThatTrue) {
   if(wasThatTrue){
      return "Yes, that was true";
  }else{
    return "No, that was false";
  }  
}

function trueOrFalse(wasThatTrue) {
   if(wasThatTrue){
      return "Yes, that was true";
  }else if(!wasThatTrue){
    return "No, that was false";
  }  
}

function trueOrFalse(wasThatTrue) {
    return wasThatTrue ? "Yes, that was true": "No, that was false";
}

From the browser information provided it appears that you are using Firefox. The FCC testing software is only consistently reliable in Chrome browser so that may be your problem. You could try refreshing the page or clearing your browser cache to get that challenge to pass but ultimately you will need to use Chrome for the camp challenges.