4

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?

9
  • 2
    What do you mean by "does nothing"? Does the function not get called or does it not do what you're expecting it to? What is the function -supposed- to do (posting the code would be useful!) that it doesn't? Commented Dec 5, 2011 at 15:54
  • 4
    And what does capturePhoto() do? Unless your JS/browser install is totally hosed, or either of those yes strings are ninjas pretending to be yesses, there's no way that this code could NOT call capturePhoto. Commented Dec 5, 2011 at 15:54
  • 2
    A side note: you might want to use true/false boolean constants rather than "yes"/"no" strings. You could then format your if as if (photo) capturePhoto();. Commented Dec 5, 2011 at 15:55
  • 8
    Why do people ask for help with their code, but then don't provide the code? Commented Dec 5, 2011 at 15:56
  • 3
    I can't update RightSaidFred's comment enough. POST YOUR DAMN CODE PEOPLE! Commented Dec 5, 2011 at 15:58

6 Answers 6

23

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!");
}

Sign up to request clarification or add additional context in comments.

5 Comments

It shouldn't be written 'It works!' it should be written - 'thank you StackOverflow!' !
@Aleks Why should the code thank StackOverflow if we haven't done anything except saying that capturePhoto() could have a bug? BTW this is more than a year old ;)
Because javascript doesn't report errors like we would want :) And even it is old more then a year, mentioning a word - bug stopped me for a second, and I figured a solution for my similar javascript problem not running a function. Even it is a year old I had to say it helped me +1 ;)
@Aleks I'm glad that I could point you in the right direction :) Thank you, thank StackOverflow (see edit)! ;)
hehe :D it is absolutely true!! ;) thanks, have a happy programming ;)
3

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

Comments

1

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/

Comments

1

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.

Comments

1

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
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.