From: David Mark on
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.
From: David Mark 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?

Bad style. One end splices an array, the other increments a variable.
That's really ugly.

>
> 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;
> }

There you go. :) Isn't that much easier on the eyes? You are missing
a few brackets though. Despite a few clunkers, most of what JSLint
tells you is good advice.

>
> 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;
> }

I don't care for that one (because of the way the assignment to e is done).
From: Stefan Weiss on
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.


--
stefan
From: Ry Nohryb on
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 ?
--
Jorge.
From: David Mark on
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.