From: Ry Nohryb on 9 May 2010 05:05 On May 8, 9:56 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote: > (...) > function removeThrees(array) { > var b = a.slice(), > j = 0, > i = j, > el; > for(; i < b.length; i++) { > el = b[i]; > if(el !== 3) { > a[j++] = el; > } > } > a.length = j; > return a; > > } > > I can't see how using `splice` in a forwards loop would be a problem at > all. I find that approach to be easy to understand and I use it in my > own code. LOL. No wonder the FAQ is as it is. function removeThrees (a) { var i= 0; while (i < a.length) a[i] === 3 ? a.splice(i, 1) : i++; return a; } -- Jorge.
From: David Mark on 9 May 2010 05:15 Ry Nohryb wrote: > On May 9, 4:14 am, David Mark <dmark.cins...(a)gmail.com> wrote: >> Garrett Smith wrote: >> >> [...] >> >> >> >>> How about a forward loop using splice? >>> function removeThrees(a) { >>> for(var i = 0; i < a.length; i++) { >>> if(a[i] === 3) { >>> a.splice(i,1); >>> } >>> } >>> return a; >>> } >> I just don't care to evaluate the length property each time through (or >> to splice one member at a time), even in an example. And though >> slightly longer, I consider mine to be easier to understand at a glance. >> YMMV. >> >> The strict comparison is a good idea in this case though. > > Hey, that code skips over any item coming after a 3... geniuses! I didn't write it (or review it), jackass!
From: David Mark on 9 May 2010 05:17 Garrett Smith wrote: > David Mark wrote: >> Garrett Smith wrote: >>> David Mark wrote: >>>> Garrett Smith wrote: >>>> > > [...] > >>> The array filter approach not as fast, as is to be expected, however it >>> is the simplest of all, with cyclomatic complexity of 2. >> >> I think you are oversimplifying. For one, it depends very much on the >> data in this case. Only way to be sure is to benchmark it with various >> data sets. I can't say as I'm interested enough to do it though. > How does cyclomatic complexity depend on the data? > > [1,2,3,4,5,2,4,21,7,4,2,8].filter(function(i){ > return i !== 3; > }); > I was referring to your assertion (apparently based on this principle) that yours was faster than mine, which is an oversimplified generalization. It clearly depends on the data.
From: David Mark on 9 May 2010 05:21 Ry Nohryb wrote: > On May 8, 9:56 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote: >> (...) >> function removeThrees(array) { >> var b = a.slice(), >> j = 0, >> i = j, >> el; >> for(; i < b.length; i++) { >> el = b[i]; >> if(el !== 3) { >> a[j++] = el; >> } >> } >> a.length = j; >> return a; >> >> } >> >> I can't see how using `splice` in a forwards loop would be a problem at >> all. I find that approach to be easy to understand and I use it in my >> own code. > > LOL. No wonder the FAQ is as it is. > > 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.
From: Ry Nohryb on 9 May 2010 05:58
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. -- Jorge. |