jcrowson
jcrowson

Reputation: 4290

call function using if statement

I want to be able to call a function within an if statement.

For example:

var photo = "yes";

if (photo=="yes") {

    capturePhoto();

}

else {
  //do nothing
}; 

This does nothing though. The function is clearly defined above this if statement.

Edit: Wow, downboated to hell! capturePhoto(); was just an example function that didn't really need any more explanation in this scenario?

Upvotes: 4

Views: 115204

Answers (6)

Smally
Smally

Reputation: 1684

You should also consider using true and false instead of strings that could be manipulated depending on input.

If I had to correct the following code, then I should've done it like this;

var photo = true; // Will capture picture.
if (photo) { // 'true' is a truthy value.
    capturePhoto();
} else {
  // Do nothing
}

Upvotes: 1

ComFreek
ComFreek

Reputation: 29424

That should work. Maybe capturePhoto() has a bug? Insert an alert() or console.log():

var photo = "yes";
if (photo == "yes") {
 alert("Thank you StackOverflow, you're a very big gift for all programmers!");
 capturePhoto();
} else {
  alert("StackOverflow.com must help me!");
}

Upvotes: 23

TimWagaman
TimWagaman

Reputation: 118

I'm not seeing any problems here. I used this code and the function call worked. I kept your code and just added a function called capturePhoto().

Are you sure that the code you're using to call the function is firing?

var photo = "yes"; 
if (photo=="yes") 
{ 
    capturePhoto(); 
} 
else 
{ 
    //do nothing 
};
function capturePhoto() 
{ 
    alert("Pop up Message"); 
}

Upvotes: 3

Joey Adams
Joey Adams

Reputation: 43380

The code looks fine to me (except you don't need the ; at the end of the last line). Check your error log; perhaps the browser thinks capturePhoto is not defined for some reason. You can also add alert statements to make sure the code is actually running:

var photo = "yes";

alert('Entering if statement');

if (photo=="yes") {
    alert('then');
    capturePhoto();
} else {
    alert('else');
    //do nothing
}

When you encounter a situation where it seems like a fundamental language feature is not working, get some more information about what is going on. It is almost never the platform's fault. It is occasionally a misunderstanding of how the feature works (e.g. why does parseInt('031') == 25 ?). It is usually a violation of an assumption you're making about the code that isn't holding up because of a problem elsewhere.

Upvotes: 1

Guffa
Guffa

Reputation: 700192

The code that you posted does work.

I copied it and tested it.

Demo: http://jsfiddle.net/Guffa/vraPQ/

The only thing wrong with it that I can see is a semicolon after the closing bracket, but that is only a style problem. It will form an extra empty statement, but that doesn't cause any problems.

Upvotes: 0

Abdul Munim
Abdul Munim

Reputation: 19217

You probably missed something, a quotation, a semicolon or something like that. I would recommend you to use a debugger like Firebug or even Google Chrome's Web Developer Tool. You will know what's wrong with your code and where it is wrong.

You may take a look at this live code that your code above works: http://jsfiddle.net/ZHbqK/

Upvotes: 1

Related Questions