freeCodeCamp Challenge Guide: Use Conditional Logic with If Statements

Use conditional logic with If statements


Hints

Hint 1

Your if statement will evaluate whether your (condition) is true or false and execute (if it evaluates to true) the {statement} declared right after it.

Hint 2

In case your (condition) evaluates to false the {statement} won’t be executed and function will return the next return statement.


Solutions

Solution 1 (Click to Show/Hide)
// 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.
}

Code Explanation

The function first evaluates if the condition (wasThatTrue) evaluates to true. If it does, ir returns the statement between the curly braces. If it doesn’t, it returns the next return statement outside them.

Relevant Links

3 Likes

There seems to be a bug here.

function myFunction(wasThatTrue) {

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

  // Only change code above this line.

}

This is correct isn’t it?

1 Like

Hello campers,can someone show me exactly why the challenge is not passing?

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

1 Like

Yes, it’s correct. However to pass the challenge you need to type the strings exactly like it states. So you need to have it as “Yes, that was true” and “No, that was false”

3 Likes

You need a space after the comma between Yes, and that. The strings have to be exactly like it states in the challenges to pass them.

4 Likes

Thank you very much.You made me realize my mistake.

Could you be having a clue on how to correct the challenge on "Return a value from a Function with Return"?

1 Like

// Example
function ourTrueOrFalse(isItTrue) {
if (isItTrue) {
return “Yes, it’s true”;
}
return “No, it’s false”;
}

// Setup
function trueOrFalse(myVal) {

// Only change code below this line.
if(myVal>8){
return “Yes, that was true”;
}
return “No, that was false”;

// Only change code above this line.

}

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

This is my code, is it correct or not? It’s not showing correct.

Hi @KaranPato,the parameter should be (myVal),not (myVal>8) as shown in your condition.Like this:

if(myVal) {
return "Yes, that was true";
}
return "No, that was false";
1 Like

@KaranPato look at this:


// 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);
7 Likes

These challenges require that one does the right thing - follow to the letter, the instructions. Check and try again. Happy coding!

1 Like