From: VK on
On May 7, 11:24 pm, VK <schools_r...(a)yahoo.com> wrote:
> In the first case it is static in VBA sense.

Ahgr... Got mixed up with those endless "static" meanings... Static in
Java sense of course, meaning that all instances of a given class
share the same single method instance provided by the constructor.
From: nick on
On May 9, 5:21 am, David Mark <dmark.cins...(a)gmail.com> wrote:
> [...]
> Ry Nohryb wrote:
> > [...]
> > function removeThrees (a) {
> >   var i= 0;
> >   while (i < a.length) a[i] === 3 ? a.splice(i, 1) : i++;
> >   return a;
> > }

> Terrible.  Don't use the ternary operator in that fashion.

Why not?

I'm not trying to sound rude, I am genuinely curious. I have almost
never seen the ternary operator used other than to the right of an
assignment operator, with the exception of two times, both recently...
once just now, and once in VK's ggNoSpam.

I just double-checked whether this kind of thing works in C++; it
seems to work fine there too.

So, I'm curious why you say not to do it. Is it because it's an
unusual thing to do and therefor difficult to read, or for a more
technical reason? I think I would naturally avoid using it like that
(not as part of an assignment), but only because it's something you
just don't see a lot.

For sparse arrays, I like:

function removeThrees (a) {
for (var i=0; i<a.length;)
if (a[i]===3) a.splice(i, 1); else i++;
return a;
}

For dense arrays, this will also work:

function removeThrees (a) {
var i=0, e;
while (typeof (e=a[i])!='undefined')
if (e===3) a.splice(i, 1); else i++;
return a;
}
From: Garrett Smith on
nick wrote:
> On May 9, 5:21 am, David Mark <dmark.cins...(a)gmail.com> wrote:
>> [...]
>> Ry Nohryb wrote:
>>> [...]
>>> function removeThrees (a) {
>>> var i= 0;
>>> while (i < a.length) a[i] === 3 ? a.splice(i, 1) : i++;
>>> return a;
>>> }
>
>> Terrible. Don't use the ternary operator in that fashion.
>
> Why not?
>
> I'm not trying to sound rude,
[...]
Didn't sound so at all.

[...]

> So, I'm curious why you say not to do it. Is it because it's an
> unusual thing to do and therefor difficult to read, or for a more
> technical reason? I think I would naturally avoid using it like that
> (not as part of an assignment), but only because it's something you
> just don't see a lot.
>

Stylistically the long condition with the ternary operation is harder to
read.

> For sparse arrays, I like:
>
> function removeThrees (a) {
> for (var i=0; i<a.length;)
> if (a[i]===3) a.splice(i, 1); else i++;
> return a;
> }
>

That fixes the consecutive threes bug in the one I wrote (ouch).

> For dense arrays, this will also work:
>
> function removeThrees (a) {
> var i=0, e;
> while (typeof (e=a[i])!='undefined')
> if (e===3) a.splice(i, 1); else i++;
> return a;
> }

Another try:

function removeThrees (a) {
var i=0;
while (i in a) {
if (a[i] === 3) {
a.splice(i, 1);
} else {
i++;
}
}
return a;
}
--
Garrett --> Needs to get out for a bit.
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: nick on
On May 9, 3:44 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
> nick wrote:
> > On May 9, 5:21 am, David Mark <dmark.cins...(a)gmail.com> wrote:

> >> Terrible.  Don't use the ternary operator in that fashion.

> > Why not?

> Stylistically the long condition with the ternary operation is harder to
> read.

Thanks, I figured it was something like that.

>
> > For sparse arrays, I like:
>
> >   function removeThrees (a) {
> >     for (var i=0; i<a.length;)
> >       if (a[i]===3) a.splice(i, 1); else i++;
> >     return a;
> >   }
>
> That fixes the consecutive threes bug in the one I wrote (ouch).

Yeah, I thought that looked a little too short when you wrote it, I'm
glad someone checked it ;)

> > For dense arrays, this will also work: [...]

> Another try:

> function removeThrees (a) {
>    var i=0;
>    while (i in a) {
>      if (a[i] === 3) {
>        a.splice(i, 1);
>      } else {
>        i++;
>      }
>    }
>    return a;}

'in' does seem a lot better there than the typeof undefined check.

What about combining the sparse and dense methods together like this?
Might be useful if evaluating 'x.length' happens to be slower than
evaluating 'y in x'. Also will work with array-like objects with no
length property (like the dense method), although that's probably not
really too useful.

function removeThrees (a) {
for (var i=0; i in a || i < a.length)
if (a[i]===3) a.splice(i, 1); else i++;
return a;
}
From: nick on

>     for (var i=0; i in a || i < a.length)

oops, that should be:

    for (var i=0; i in a || i < a.length;)