'What does while (i --> 0) mean?
I apologize if this a stupid question, but I cannot find the answer anywhere.
How does the following code work? (I realize that it loops over the elements of els)
var i = els.length;
while (i --> 0) {
var el = els[i];
// ...do stuff...
}
I have no idea what --> means. There is no documentation for it. Can someone enlighten me?
Solution 1:[1]
It's just weird spacing, should be
while((i--) > 0)
it's just post-decrementing and checking the condition. There was this humorous answer at the C++ question, but I think it got deleted
while (x --\
\
\
\
> 0) //i goes down to zero!
Or something like that, anyway
So if you had something like
var i=3;
while(i-->0){
console.log(i);
}
it would return
2
1
0
Solution 2:[2]
The code should actually be:
while (i-- > 0) {
where the loop will run if the value after the variable i has been decremented is greater than zero.
Solution 3:[3]
It's just weird spacing. It's same as
while (i-- > 0) {
Solution 4:[4]
while (i--> 0)
{
// ...do stuff
}
is same as
while (i>0)
{
i--;
// ...do stuff
}
IMHO we should write simple code rather than clever code because it's not understandable by everyone.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Stoic |
| Solution 3 | Samuel Neff |
| Solution 4 | Chirag |
