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?

jcrowson
  • 4,290
  • 12
  • 54
  • 77
  • 1
    Let's see the function definition. – aziz punjani Dec 05 '11 at 15:54
  • 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? – Anthony Grist Dec 05 '11 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. – Marc B Dec 05 '11 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();`. – Xion Dec 05 '11 at 15:55
  • 8
    Why do people ask for help with their code, but then don't provide the code? – RightSaidFred Dec 05 '11 at 15:56
  • 1
    What *Xion* said. Using `true` and `false` it making the code more readable and the execution is faster. If you are retrieving in your real code `"yes"/"no"` from user then just convert it to boolean `var photo = input === "yes";`. – kubetz Dec 05 '11 at 15:57
  • 3
    I can't update RightSaidFred's comment enough. *POST YOUR DAMN CODE PEOPLE!* – Polynomial Dec 05 '11 at 15:58
  • @RightSaidFred - It's clearly defined. Don't worry about it. – Jeff Camera Dec 05 '11 at 16:01
  • 1
    LordSnoutimus: You've provided nothing of use in your question. If you really don't know if the `if` test should work, then you need to read a very basic programming tutorial. If you're having an issue in your actual code, then **you need to provide the code that is giving you the issue**. If you genuinely believe that the problem is with the `if`, and not your other code, then you need to learn some basic troubleshooting skills because they would lead you to the conclusion that the `if` will work properly. This question deserves far more downvotes than it has received. – RightSaidFred Dec 05 '11 at 16:31

6 Answers6

22

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!");
}
ComFreek
  • 29,044
  • 18
  • 104
  • 156
  • 2
    It shouldn't be written '`It works!`' it should be written - '`thank you StackOverflow!`' ! – Aleks Mar 26 '13 at 14:48
  • 1
    @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 ;)* – ComFreek Mar 26 '13 at 14:52
  • 1
    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 Mar 26 '13 at 15:14
  • 1
    @Aleks I'm glad that I could point you in the right direction :) Thank you, thank StackOverflow (see edit)! ;) – ComFreek Mar 26 '13 at 15:24
  • 1
    hehe :D it is absolutely true!! ;) thanks, have a happy programming ;) – Aleks Mar 26 '13 at 15:34
2

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"); 
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
TimWagaman
  • 108
  • 7
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
}
Smally
  • 1,556
  • 1
  • 8
  • 27
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/

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
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.

Community
  • 1
  • 1
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005