+ 2

Help with if statement!

I want to do something like this: if(functionName()==true) { //do this } I didn't think something like this was gonna work but I don't know how to do this.

2nd May 2022, 7:33 AM
Spencer Pawlik
Spencer Pawlik - avatar
11 Answers
+ 4
yeah it will worked, Now you just need to define the function, but make sure it should return a boolean value or in terma of numbers ( 0 = false, or any other number is for true ) function main() { if(functionName() == true) // 👈 calling the function console.log("worked") } // function defination 👇👇👇👇 function functionName() { return true; }
2nd May 2022, 8:18 AM
NonStop CODING
NonStop CODING - avatar
+ 3
it will work if function returns true
2nd May 2022, 8:30 AM
Vishal
Vishal - avatar
+ 2
Lego in Motion Why you don't think? If function will return true then definitely if condition will execute.
2nd May 2022, 8:51 AM
A͢J
A͢J - avatar
+ 2
Subhadeep Mandal What about this: bool isEven(int num) { return num % 2 == 0; }
2nd May 2022, 4:55 PM
A͢J
A͢J - avatar
+ 2
it will work if function returns boolean
3rd May 2022, 6:53 PM
Zakaria Elalaoui
Zakaria Elalaoui - avatar
0
The way I understand if statement, When an if is declared Like this if (here you write the condtion ie 5===5) Now if the condition is true then {only will the code in here will be executed}
2nd May 2022, 11:20 AM
William Mwale
William Mwale - avatar
0
bool isEven(int num){ if (num%2==0) return true; else return false; } //wherever you want to apply if(isEven(6)) //do this
2nd May 2022, 4:47 PM
Subhadeep Mandal
Subhadeep Mandal - avatar
0
A͢J I never said I don't think that.😂 thanks for the help but it's not really working. So I just need to put: return true; ?
2nd May 2022, 5:16 PM
Spencer Pawlik
Spencer Pawlik - avatar
0
It lookes like js, if statement on js is like this if (function == 1) {what happens}
3rd May 2022, 6:53 PM
Chris
Chris - avatar
- 1
looks like you still have same confusion so i created this simple example i hope it will solve your problem https://code.sololearn.com/Wh02DZdl19V3/?ref=app
2nd May 2022, 6:36 PM
Vishal
Vishal - avatar