 # freeCodeCamp Challenge Guide: Use Conditional Logic with If Statements

freeCodeCamp Challenge Guide: Use Conditional Logic with If Statements
0

#1

We can use `if` statements in JavaScript to execute code only if the specified condition is met.

Each `if` statement requires a `boolean` condition to evaluate. If the boolean evaluates to `true`, the statements inside the curly braces will execute. Otherwise, if it evaluates to `false`, the code will not execute.

## Example

``````function trueOrFalse(myVal) {
if (myVal > 10) {
return "Greater Than";
}
return "Not Greater Than";
}
``````

If `myVal` is greater than `10`, the function will return `"Greater Than"`. If it is not, the function will return `"Not Greater Than"`. Both `return` statements will never get executed because as soon as the first one is encountered, the execution exits the function and returns control to where the function was called.

HERE IS THE CODE FOR IT.
PAY ATTENTION TO THE INSTRUCTIONS ,DON’T JUST READ: UNDERSTAND IT FIRST !
// 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);

Javascript challenge Use Conditional Logic with If Statements
#2

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?

#3

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

#4

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”

#5

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.

#6

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

#7

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

#8

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

#9

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";``````

#10

@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);``````