0

I have a for-loop that is terminating without finishing the loop. It seems to be related to whether or not a call to another function is made within the loop.

this.cycle = function() {
    var list = this.getBreaches("Uncontained");
    if (list.length > 0) {
        for (i=0; i < list.length; i++) {
           this.saveVsRupture(DC=11, i);   //calls this.rupture() if save failed
    }}
    return 1;
};

this.saveVsRupture() calls a function that rolls a d20 and checks the result. If the save fails, it calls a method called this.rupture() that does some adjusting to this.

Problem If the saving throw is successful, the loop continues, but if the saving throw fails, it runs the this.rupture() function and then breaks the for-loop. It should keep running the for-loop.

Why is this happening?

Additional Details Here are the other functions...

    savingThrow = function(DC=11) {
    // DC = Difficulty Check on a d20   
    try {
        if (0 <= DC) {      
            var roll = Math.floor((Math.random() * 20))+1;  //roll d20
            var msg = "(Rolled "+roll+" vs DC "+DC+")";
            console.log(msg);       
            if (roll >= DC) {   //Saved
                return true;
            }
            else {  //Failed save
                return false;
            }
        }
    }
    catch(e) {
        console.log("Exception in savingThrow: "+e);        
    };
  };

    this.saveVsRupture = function(DC=1, i=null) {
    try {
        if (!savingThrow(DC)) {
            this.rupture(i);
            return false;
        }
        return true;
    }
    catch(e) {
        console.log(e);         
        }   
};


    this.rupture = function(i=null) {
    if (i == null) {
        i = range(1,this.damageList.length).sample();       
    }
    var hole = this.damageList[i];
    var dmg = range(1,this.harmonics()).sample();
    hole.rupture(dmg);
    msg = "*ALERT* " + hole + " expanded by " + dmg + "% Hull Integrity @"+this.hullIntegrity();
    this.log(msg);
    if (hole.size % 10 == 0) {
        this.health -= 25;
        msg = "The ship creaks ominously.";
        this.log(msg); 
    }
    return 1;
};  
5
  • 1
    Most likely the rupture code is throwing an error, either explicitly or due to some error in the code. To work around that, wrap the saveVsRupture call in a try/catch block. Commented Dec 6, 2015 at 5:15
  • I have tried try/catch on all the functions involved and none of them are throwing any exceptions. Should I post more code? Commented Dec 6, 2015 at 5:28
  • What browser do you use? post your saveVsRupture code if possible. Commented Dec 6, 2015 at 5:32
  • Using Firefox 42.0 on Enlightment OS Luna / Ubuntu Commented Dec 6, 2015 at 5:34
  • using chrome devTools sometimes gives you better result, what saveVsRupture function does it do? Commented Dec 6, 2015 at 5:39

3 Answers 3

2

The correct syntax for the for-loop declares the counter variable.

    for (var i=0; i < list.length; i++) {etc..

    /// Causes the For-Loop to exit prematurely...
    for (i=0; i < list.length; i++) {etc..

Once the "var i=0" is used, the for-loop operates as expected.

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

1 Comment

Also, considering the method of this loop, it's really just iterating over an array and Javascript 6 provides specific methods for iterating over arrays. Try using: Array.forEach()
0

consider implementing a try-catch in your for loop, with your saveVsRupture function within the try. This implementation will catch errors in your function but allow the program to keep running.

Comments

0

Change the saveVsRupture function like this:

function saveVsRupture(a,b) {
    try{
        //your saveVsRupture code here...
    }
    catch(e){}
}

Your should catch problems that occurred in your code with try,catch block to prevent throw them to top level of your code (the browser in this example) .

Don't use return in for loop!

Change your code as following:

this.cycle = function() {
    var list = this.getBreaches("Uncontained");
    if (list.length > 0) {
        for (i=0; i < list.length; i++) {
            var temp = this.saveVsRupture(DC=11, i);   //calls this.rupture() if save failed
            console.log(temp);
        }}
    return 1;
};

2 Comments

When I run this, it says that the saveVsRupture function is returning "false" when it breaks the loop. ie, it returns true as long as the loop is running but as soon as temp=false the loop is broken. I don't understand why that is.
this.cycle = function() { var list = this.getBreaches("Uncontained"); if (list.length > 0) { for (i=0; i < list.length; i++) { console.log("counter i ->"+i); var temp = this.saveVsRupture(DC=11, i); //calls this.rupture() if save failed console.log(temp+ " !!! "+i); } Running this reveals that var i (the counter) is being set to 3 whenever the saveVsRupture comes back as false, even if i was 0 before it. I cannot yet see why this would be.

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.