What?! Use Conditional Logic with If Statements

What?! Use Conditional Logic with If Statements
0

#1

Tell us what’s happening:
Okay, so I got this to work. It was easy. Stupidly easy BUT, apparently I’m stupid for not knowing WHY it works. And if I move on without someone explaining to me why the hell this works I may go nuts.

So, walking through this. I declared a function gave it a parameter. Then if my parameter is true i get the first return, if false i get the second. Seems simple enough. But where does our function ever tell the thing to check if it is true. All i’m efdectively doing when i run it is giving my parameter a value of either true or false, so i should just be getting back my first line. Computer should see it be like yup, it’s got a value, return his first line.

Shouldn’t it be more like this?

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

What is my function comparing the argument to in order to give me either return? If I go with the answer they want it doesn’t make sense. What am i missing?

Your code so far

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

// Setupo
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(true);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Linux; Android 7.0; SM-G955U Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.123 Mobile Safari/537.36.

Link to the challenge:


#2

if statements are always going to treat their conditions as either true or false (actually “truthy” or “falsey”, but that’s for another day). The values true and false are special. They aren’t just the words "true" and "false", but the only two options of a value type called a Boolean. It’s like heads or tails, on or off, yes or no, 1 or 0.

There are expressions that evaluate to true or false, like true === true. Usually you are putting something like this in a conditional. But you can also just take a true or false directly. For this reason, you never have to use === true inside a conditional.


#3

In your first example, the = creates an assignment and is not a comparison operator. I think you meant === which is for comparing. Replace the = with === and it would work.

In the following example (from above), the if (wasThatTrue) { works the same, because if statements expect to evaluate an expression as true or false.

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(true);

This solution takes advantage of something you have not yet learned about (as alluded to by @ArielLeslie). Basically, any value that is not false, undefined, null, NaN, or 0 (the number) gets evaluated by the if statement as true. If the value in one of these five I just mentioned, then it gets evaluated as false by the if statement. In the above example, since you pass in true to the function, then the argument/variable wasThatTrue is true and true gets evaluated as true, because it is not one of those five falsey values I mentioned above.

I told you it was more complex. You will get to that challege where it discussed this in the Basic Algorithm Scripting section.


#4

Thank you both. Also thank you for the correction on === vs =. I knew there had to be a reason behind why it worked, but couldnt wrap my brain around it. I appreciate your help very much. I feel really dumb when stuff that seems so simple confuses me :frowning:


#5

Don’t feel dumb. @RandellDawson and I (and others) are here because when people are learning to code, they need to ask questions like this.