9
Is there any way I can do that? As long as it’s one thing to do that, then when it’s another to do that?
For example:
var i = 0;
while(i < 5){
//faça isso
i++;
} else {
//faça aquilo
}
It’s possible to do it somehow?
9
Is there any way I can do that? As long as it’s one thing to do that, then when it’s another to do that?
For example:
var i = 0;
while(i < 5){
//faça isso
i++;
} else {
//faça aquilo
}
It’s possible to do it somehow?
15
In JS does not exist, you need to create a flag inside the loop for a if
be executed or not after the end of this loop. Or think of a different stream that doesn’t need this.
The example of the question is not good because it does not need this algorithm. Actually this can be solved with a for
simple. If the condition data were unknown it could do something like this:
var entrada = 1; //teria que ser uma valor desconhecido
var entrou = false;
while (entrada < 10) {
entrou = true;
//faz algo mais aqui, caso contrário não teria sentido
entrada++;
}
if (!entrou) {
console.log("deu problema");
}
console.log("vamos tentar de novo");
var entrada = 11; //teria que ser uma valor desconhecido
var entrou = false;
while (entrada < 10) {
entrou = true;
//faz algo mais aqui, caso contrário não teria sentido
entrada++;
}
if (!entrou) {
console.log("deu problema");
}
I put in the Github for future reference.
Beware of solutions that use the condition itself to decide whether to perform or not, or whether to use an inverted condition after the loop. This tends to generate bugs with maintenance. This violates the DRY. Even at first it can generate a bug and not realizing.
Depending on the condition you can call a function that has a side effect or provoke a racing condition and produce an unexpected result. It’s likely that you test and it works. But one day something happens that doesn’t work since it’s not right. Working and being right are two different things.
-1
The way you wrote the code doesn’t exist Else javascript.
I suggest rewriting your code another way, for example:
while(i < 5){
//faça alguma coisa
}
if(i >= 5){
//faça outra couisa
}
I don’t know if this is the action that should be taken. If it’s not better explain to us what you want the code to do.
Take a look at me to understand the problem this could bring.
Just put the i++
within the while
that you will see the problem.
It’s true. This is an atypical situation, at least for me. I don’t remember a situation where I needed a while {} ... else
.
Browser other questions tagged javascript loop else
You are not signed in. Login or sign up in order to post.
Has not
else
for a repeat loop.– rray
can explain better what would happen inside the block
else
? Because for me if you remove theelse
already resolves.– mercador
@merchant, I edited!
– Lucas de Carvalho
It’s not simpler just to reverse the condition?
if(..... > 1){ executa a função }
I don’t know what the while purpose is in that code.– rray
As rray says There is no
else
for repeat loops and probably you don’t need it. I believe a simpleIf
should solve your problem, what we need to better understand is how you create the element#recomendation-sizebay-span
to know where to put theif
or the function callarrumaSzb()
.– Caique Romero
So that’s the problem, this #recomendation-sizebay-span comes through Google’s GTM, and it’s always the last element loaded on the page. If I use IF, the length will always give 0, then it is no use running the function if the #recomendation-sizebay-span does not exist yet.
– Lucas de Carvalho
Google Tag Manager
– Lucas de Carvalho
If it is a google function or script there must be some kind of call back, cites the use of GTM in your question and includes the code. A good start is to take a look at the google documentation in order to check if there is already something implemented.
– Caique Romero
It does not seem to me a problem unique to GTM to the point of needing tag or mention. I believe the question applies to any programming context.
– Bacco
I didn’t mean to add a tag, but I find it interesting to have in the question the code that creates the element that should be "observed", there are other ways to constantly check the existence of it but I don’t think it is the best solution.
– Caique Romero
I reversed his question because it completely alters the original meaning and invalidates the answer already given and voted. If you have a different question, ask a separate question.
– Bacco