0

Possible Duplicate:
For each in a array. How to do that in javascript?

Hey I was needing some help with finding a good for each equivalent from PHP to javascript. I've tried using jQuery.each() but I'm not sure I've done it correctly.

Here is the code I've been needing to convert. I need to fix each foreach to javascript.

if(calculate){
    // got complete, calculate possible solution
    // use decimals?  Does this work in the form?
foreach(values as valName1 => val1){
    foreach(operands as op1){
        foreach(values as valName2 => val2){
            if(valName2 == valName1)
                continue;
            foreach(operands as op2){
                foreach(values as valName3 => val3){
                    if(valName3 == valName1 ||
                       valName3 == valName2)
                        continue;
                    foreach(operands as op3){
                        foreach(values as valName4 => val4){
                            if(valName4 == valName1 ||
                               valName4 == valName2 ||
                               valName4 == valName3)
                                continue;
                            foreach(operands as op4){
                                foreach(values as valName5 => val5){
                                    if(valName5 == valName1 ||
                                       valName5 == valName2 ||
                                       valName5 == valName3 ||
                                       valName5 == valName4)
                                        continue;
                                    parentheses = -1;
                                    if(solve(val5, op4, val4, op3, val3, op2,
                                             val2, op1, val1, solution, parentheses)){
                                        count++;
                                            // Note that since solve() just returns the first result, we won't
                                            // display all possible parentheses ordering when there are multiple
                                            // solutions
                                        if(find_all){
                                            document.write(Solution(val5, op4, val4, op3, val3, op2,
                                                          val2, op1, val1, solution, parentheses, find_all);
                                        }
                                        else{
                                            document.write(Solution(val5, op4, val4, op3, val3, op2,
                                                          val2, op1, val1, solution, parentheses, find_all);
                                            break;
                                        }
                                    }
                                    parentheses = -1;
                                }
                                parentheses = -1;
                                if(!find_all && count) break;
                            }
                            if(!find_all && count) break;
                        }
                        if(!find_all && count) break;
                    }
                    if(!find_all && count) break;
                }
                if(!find_all && count) break;
            }
            if(!find_all && count) break;
        }
        if(!find_all && count) break;
    }
    if(!find_all && count) break;
}

if(!count || find_all){
    document.write( "Found " . count . " solutions.";
}

}
Community
  • 1
  • 1
  • 10
    Wow. What on Earth is *that* code doing?! – Martin Bean Dec 10 '12 at 14:28
  • 1
    Yikes man, just yikes! If code gets that deeply nested it's the code's way of telling you to rethink your approach! – GordonM Dec 10 '12 at 14:31
  • without knowing what the code does, I know one thing: it can be much more simple and elegant – Matanya Dec 10 '12 at 14:32
  • 1
    One month from now, no one on earth will know what that code is doing. Consider splitting it up into several functions and/or use recursion. At the very least this code needs a LOT of comments if anyone is ever going to have to change it later (which is almost a certainty). – Joe Dyndale Dec 10 '12 at 14:44

1 Answers1

2

If you are looking for functions (eg. foreach) dealing with data-sets the underscore.js is a good choice.

glglgl
  • 89,107
  • 13
  • 149
  • 217
sarveshseri
  • 13,738
  • 28
  • 47