Announcement

Collapse
No announcement yet.

problem with functions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem with functions

    Who can help me out with the following problem. Here is a simplified version of my code.

    //global
    var s

    function preMain() {
    ...
    }

    function main() {
    ...
    if (condition w) {
    debugPrintln("test 1")}
    ...
    if (condition x) {...}
    else {s=fSample();
    debugPrintln("test 2" + s)}
    }
    ...
    return ...;
    }

    function fSample() {
    var i=-1;
    if (condition y) {
    i=1;
    while (condition z) {...}
    ...
    }
    return i;
    }

    I discovered in the formula output window, that in the case of failing condition y, function fSample is called for twice, right after each other (1 time too much!--> error 1). If condition y is met, s is given the value i, but the debugPrintln("test 2" +s) is never called for. The program seems to jump right to debugPrintln("test 1") again (--> error 2). And that without any loops, which could cause this, being present!

    Could someone please explain me what's happening here?

    Bart

  • #2
    Hello Bart,

    Welcome to the community. Take a look at the following setup of your example.

    PHP Code:
    function preMain() {
        
    setStudyTitle("test");
        
    //setPriceStudy(true);
        
    setShowCursorLabel(false);

    }

    //global
    var s

    debugClear
    ();

    function 
    main() {
        var 
    condition_w true;
        var 
    condition_x false;
        
    //...
        
    if (condition_w) {
            
    debugPrintln("test 1")
        }
        
    //...
        
    if (condition_x) {
            
    //...
        
    } else {
            
    fSample();
            
    debugPrintln("test 2: " s)
        }
        
    //...
        
    return //...;
    }

    function 
    fSample() {
        var 
    condition_y true;
        var 
    condition_z false// don't set this to true!
        
        
    var = -1;
        if (
    condition_y) {
            
    1;
            while (
    condition_z) {
                
    //...
            
    }
            
    //...
        
    }
        
    debugPrintln("fSample executed");
        return 
    i;

    Based on this code, I'm only seeing fSample() getting called once per execution of the formula when condition_y is false. Do you have any other calls to fSample() somewhere else in your code?

    Setting condition_y to true will set i = 1. In main(), the "test 2:" debugPrintln() statement will only be called if condition_x is false. Add a debugPrintln() statement just above "if (condition_x)" and check to make sure it is false.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Jason,

      Thanks for your advice. I checked my code again; 1 of my conditions was not properly formulated. It took me a while to find it, but it finally works!

      regards,
      Bart

      Comment

      Working...
      X