1
I am implementing a code where if the received variable does not contain a value, a for
, otherwise, no.
Well, if it were for the "simple" case, it would just implement the if
with the conditions and assemble the for
as the result, but so would be very large the code. I would like to know if it is possible to do the following excerpt in Javascript.
<script language="javascript">
w_valor_1;
w_valor_2;
if(w_valor_1 == ""){
w_for_1 = "for(i = 0; i < w_b; i++){";
w_ch_1 = "}";
}
else{
i = w_valor_1;
w_for_1 = "";
}
if(w_valor_2 == ""){
w_for_2 = "for(j = 0; j < w_c; j++){";
w_ch_2 = "}";
}
else{
j = w_valor_2;
w_for_2 = "";
}
w_for_1;
w_for_2;
m_valores[i][j] = b++;
w_ch_2;
w_ch_1;
</script>
I imagine the solution is to use the val.
– Felipe Avelar
@Felipeavelar, reading what you passed on the I came up with a question. With it is possible to realize the
for
? Would he iterate normally? How to ride with him if that were my case? I’m sorry, it’s a lot of questions and.e'– Felipe
Actually, seeing your code better, I couldn’t understand where you initialize w_b and w_c, but what I would do is initialize both with zero and only change their value, if w_valor_1 and w_valor_2 were different from "" respectively.
– Felipe Avelar
In the case of
w_b
andw_c
they come fromPHP
, where it contains a value from a previous page and may contain values of 1...n!– Felipe
@Felipeavelar, is it possible to mount with Eval? If so, could you show me an example with the code I showed you? ' Thanks!
– Felipe
@mgibsonbr has already shown how, in addition to giving a better solution than using Eval, which is a little slower. (:
– Felipe Avelar