将一些PHP代码转换为javascript(foreach等效)
可能重复:将一些PHP代码转换为javascript(foreach等效),php,javascript,foreach,Php,Javascript,Foreach,可能重复: 嘿,我需要一些帮助来找到从PHP到javascript的每一个等价物的好方法。我尝试过使用jQuery.each(),但我不确定是否正确 这是我一直需要转换的代码。我需要将每个foreach修复为javascript if(calculate){ // got complete, calculate possible solution // use decimals? Does this work in the form? foreach(values as va
嘿,我需要一些帮助来找到从PHP到javascript的每一个等价物的好方法。我尝试过使用jQuery.each(),但我不确定是否正确 这是我一直需要转换的代码。我需要将每个foreach修复为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.";
}
}
如果您正在寻找处理数据集的函数(例如,
foreach
) 哇。那代码到底在做什么?!哎呀,伙计,哎呀!如果代码嵌套得那么深,那么代码就是在告诉您重新思考您的方法!如果不知道代码的作用,我知道一件事:从现在开始一个月后,它可能会变得更加简单和优雅,地球上没有人会知道代码在做什么。考虑把它分成几个函数和/或使用递归。如果以后有人要修改它,至少这段代码需要很多注释(这几乎是肯定的)。