From: Ry Nohryb on
On May 10, 10:21 am, David Mark <dmark.cins...(a)gmail.com> wrote:
> Ry Nohryb wrote:
> > On May 9, 11:21 pm, David Mark <dmark.cins...(a)gmail.com> wrote:
> >> Ry Nohryb wrote:
> >>> On May 9, 11: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.
> >>> Yeah, the ternary operator was not designed to remove threes.
> >> Don't be a twit.  Ask your buddy what he thinks of the style.
>
> > "Ask your buddy" ?  My buddy ? Who's my buddy ?
>
> You sound like a parrot.  :)
>
> Who do you think your buddy is?  Hint: lint.

No no no. It would hurt my feelings!

I often do:

-Assignments in the test part of ifs and whiles: if (a=b)
-Flow control with boolean operators: !done && continue();
-whiles and ifs without blocks: while (cond) whatever();
-other heretical constructions.

So JSLint is not for me. Thanks but no, thanks.
--
Jorge.
From: David Mark on
Ry Nohryb wrote:
> On May 10, 10:21 am, David Mark <dmark.cins...(a)gmail.com> wrote:
>> Ry Nohryb wrote:
>>> On May 9, 11:21 pm, David Mark <dmark.cins...(a)gmail.com> wrote:
>>>> Ry Nohryb wrote:
>>>>> On May 9, 11: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.
>>>>> Yeah, the ternary operator was not designed to remove threes.
>>>> Don't be a twit. Ask your buddy what he thinks of the style.
>>> "Ask your buddy" ? My buddy ? Who's my buddy ?
>> You sound like a parrot. :)
>>
>> Who do you think your buddy is? Hint: lint.
>
> No no no. It would hurt my feelings!

I was referring to Crockford of course. How can a Web page be your
buddy? :)

>
> I often do:
>
> -Assignments in the test part of ifs and whiles: if (a=b)

Add another set of parenthesis and I'll let it go.

> -Flow control with boolean operators: !done && continue();

You would.

> -whiles and ifs without blocks: while (cond) whatever();

How perfectly illegible of you.

> -other heretical constructions.

....and just look at the difference! :)

>
> So JSLint is not for me. Thanks but no, thanks.

It has many configuration options. Find one that suits you (or find a
similar tool).
From: Ry Nohryb on
On May 10, 10:52 am, David Mark <dmark.cins...(a)gmail.com> wrote:
> Ry Nohryb wrote:
>
> > -other heretical constructions.
>
> ...and just look at the difference!  :)

Look ma, no parens :

eval('0, { "a": 666 }');
eval("0, function(){}");

--
Jorge.
From: RobG on
On May 10, 9:52 am, Stefan Weiss <krewech...(a)gmail.com> wrote:
> On 09/05/10 21:44, Garrett Smith wrote:
>
> > Another try:
>
> > function removeThrees (a) {
> > var i=0;
> > while (i in a) {
> > if (a[i] === 3) {
> > a.splice(i, 1);
> > } else {
> > i++;
> > }
> > }
> > return a;
> > }
>
> That won't work for sparse arrays...
>
> var arr = [1];
> arr[2] = 3;
> console.log(removeThrees(arr)); // [1, undefined, 3]
>
> ... because 1 is not "in arr", thus ending your while loop.

The problem is not clearly specified - what should be done with the
matching elements? Should they be removed, moving all subsequent
elements to a lower index? should they be set to undefined? Should
they be removed from the array in a manner that makes their index not
enumerable and keeps the same index for subsequent elements?

e.g. to keep sparseness and length and remove 3s (hasOwnProperty
filter removed for brevity):

function remove3s_sparse(a) {
var b = [];
for (var p in a) {
if (a[p] != 3) {
b[p] = a[p];
}
}
b.length = a.length;
return b;
}

To make the filtered array dense:

function remove3s_dense(a) {
var b = [];
for (var p in a) {
if (a[p] != 3) {
b.push(a[p]);
}
}
return b;
}

A simple for loop could be used, however I seem to remember from a
distant post that accessing undefined elements of a sparse array can
cause them to become enumerable in some UAs. If that is not an issue,
then the following will set filtered elements to undefined and
maintain position and length:

function remove3s_simple(a) {
var o, b = [];
for (var i=0, iLen=a.length; i<iLen; i++) {
if (a[i] != 3) {
b.push(a[i]);
} else {
b.push(o);
}
}
return b;
}

or using the dreaded ternary operator where some think it shouldn't:

function remove3s_simple2(a) {
var o, t, b = [];
for (var i=0, iLen=a.length; i<iLen; i++) {
t = a[i];
b[i] = (t == 3)? o : t;
}
return b;
}


The dense result could also be achieved using the sparse function
followed by a compressor that removes undefined elements (i.e. where
typeof a[i] == 'undefined') whether they are enumerable or not.


The above issues arise with any array filtering function, like both
sparse and dense filters are required. The only problem remaining is
how to write them to be efficient (which doesn't necessarily mean less
code) and avoid implementation foibles (if any).


--
Rob
From: David Mark on
RobG wrote:
> On May 10, 9:52 am, Stefan Weiss <krewech...(a)gmail.com> wrote:
>> On 09/05/10 21:44, Garrett Smith wrote:
>>
>>> Another try:
>>> function removeThrees (a) {
>>> var i=0;
>>> while (i in a) {
>>> if (a[i] === 3) {
>>> a.splice(i, 1);
>>> } else {
>>> i++;
>>> }
>>> }
>>> return a;
>>> }
>> That won't work for sparse arrays...
>>
>> var arr = [1];
>> arr[2] = 3;
>> console.log(removeThrees(arr)); // [1, undefined, 3]
>>
>> ... because 1 is not "in arr", thus ending your while loop.
>
> The problem is not clearly specified - what should be done with the
> matching elements? Should they be removed, moving all subsequent
> elements to a lower index? should they be set to undefined? Should
> they be removed from the array in a manner that makes their index not
> enumerable and keeps the same index for subsequent elements?
>
> e.g. to keep sparseness and length and remove 3s (hasOwnProperty
> filter removed for brevity):
>
> function remove3s_sparse(a) {
> var b = [];
> for (var p in a) {
> if (a[p] != 3) {
> b[p] = a[p];
> }
> }
> b.length = a.length;
> return b;
> }
>
> To make the filtered array dense:
>
> function remove3s_dense(a) {
> var b = [];
> for (var p in a) {
> if (a[p] != 3) {
> b.push(a[p]);
> }
> }
> return b;
> }
>
> A simple for loop could be used, however I seem to remember from a
> distant post that accessing undefined elements of a sparse array can
> cause them to become enumerable in some UAs. If that is not an issue,
> then the following will set filtered elements to undefined and
> maintain position and length:
>
> function remove3s_simple(a) {
> var o, b = [];
> for (var i=0, iLen=a.length; i<iLen; i++) {
> if (a[i] != 3) {
> b.push(a[i]);
> } else {
> b.push(o);
> }
> }
> return b;
> }
>
> or using the dreaded ternary operator where some think it shouldn't:

If you mean me, then you misunderstood my objection to Ry's usage.

>
> function remove3s_simple2(a) {
> var o, t, b = [];
> for (var i=0, iLen=a.length; i<iLen; i++) {
> t = a[i];
> b[i] = (t == 3)? o : t;
> }
> return b;
> }
>

Nothing wrong with that operation.