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
}
console.log(trueOrFalse(true))
Why in the code above, the function does not return “No, that was false” even though the last return is return "No, that was false"; , and i am not using the else statement for the function to return the other value like for example in the code below:
function trueOrFalse(wasThatTrue) {
// Only change code below this line
if (wasThatTrue) {
return "Yes, that was true";
}else {
return "No, that was false";
}
// Only change code above this line
}
console.log(trueOrFalse(true))
Does the if statement have the priority or what, can someone explain please?
and What is the difference between writing with else and without else
From what I can see in the code your code is correct in case of false then it will return “No, That was false” but from what I can see as you are passing the true condition in the log so you are not getting the false statement.
If possible attach your challenge it might help us understand more about what problem you are facing if the challenge is failing.
if the condition is true then it returns the string and closes the function. if the IF condition fails then it returns the false statement also it will be returned in case of values other than if condtions required.
Thank you for giving your time, i understood now thanks anyway.
Solution: The return statement stops the execution of a function and returns a value from that function.
That’s why the function doesn’t keep going and return the “No, that was false”.
Hello @bedward
The return statement below the if block is considered to act as the else statement if the ELSE keyword is not provided.
The code below is the same
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
}
console.log(trueOrFalse(true))
it is the same as
function trueOrFalse(wasThatTrue) {
// Only change code below this line
if (wasThatTrue) {
return "Yes, that was true";
} else{
return "No, that was false";
}
// Only change code above this line
}
console.log(trueOrFalse(true))
We can also chain a if else while using the if statement to indicate a if else
function trueOrFalse(wasThatTrue) {
// Only change code below this line
if (wasThatTrue == true) {
return "Yes, that was true";
}
if(wasThatTrue == false){
return "No,ThatIsNotTrue"
}
return "No, value passed in the function";
// Only change code above this line
}
console.log(trueOrFalse(false)) //No,ThatIsNotTrue
The code above checks if the argument passed in the trueOrFalse Function is true then Yes,ThatWasTrue is Logged else if the argument is false then No,ThatIsNotTrue is Logged else log No, value passed in the function.