From: Johannes Baagoe on 14 May 2010 09:08 David Mark : [removeThrees] > Definitely many ways to skin this cat. What about this one, assuming that no element of a has a ToString value containing at least one comma (Numbers are safe) ? a.toString().replace(/^3,/, '').replace(/,3$/, ''). replace(/,3,/g, ',').split(','); -- Johannes
From: David Mark on 14 May 2010 10:28 Johannes Baagoe wrote: > David Mark : > > [removeThrees] > >> Definitely many ways to skin this cat. > > What about this one, assuming that no element of a has a ToString value > containing at least one comma (Numbers are safe) ? > > a.toString().replace(/^3,/, '').replace(/,3$/, ''). > replace(/,3,/g, ',').split(','); > There are several potential problems. For one, it will always end up as a dense array of strings, which is something you could stipulate in the documentation. The first two replace calls can be consolidated as well.
From: Johannes Baagoe on 14 May 2010 10:42 David Mark : > Johannes Baagoe wrote: >> a.toString().replace(/^3,/, '').replace(/,3$/, ''). >> replace(/,3,/g, ',').split(','); > There are several potential problems. Of course, it was more like a joke. I wonder how a typical job interviewer would respond, though. When I was in that situation, I never resisted the temptation to answer in a hopefully very unexpected way. If the interviewer took offence, I knew that that employer was not for me. -- Johannes
From: Garrett Smith on 14 May 2010 12:17 MikeMainFrame wrote: > On May 9, 4:14 am, David Mark <dmark.cins...(a)gmail.com> wrote: >> Garrett Smith wrote: [...] > I have followed your discussions and want to suggest this: [...] That's clearer and more efficient. > > Bubbles up the ones you need to delete - then cut off on the way > back ... > Shifts the set of good elements to the front and saves the length of that set each iteration. > I use to do mainframe programming and I was surprised the first time I > experienced the "living" length property - not used to OO ;o) > So why did you use `slice` instead of setting `length = jx` the last statement? function removeThrees(a) { var ix, jx=0, len = a.length; for (ix = 0; ix < len; ix++) { if(a[ix] !== 3) { a[jx++] = a[ix]; } } a.length = jx; return a; } > I was looking for some critisism on JQuery and the like. Found this - > and how nice it was to learn that a professional has to know the > concepts of the language ;o) Seems to be the opposite where I live. Endorsing a library has become nearly a requirement for any professional jobs here. Not a jQuery fan? Good luck. -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Ry Nohryb on 14 May 2010 18:13
On May 14, 6:17 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote: > MikeMainFrame wrote: > > I have followed your discussions and want to suggest this: > > > > function removeThrees(a) { > > var jx=0; > > for (var ix = 0; ix < a.length; ix++) { > > if(a[ix] !== 3) { > > a[jx] = a[ix] > > jx++; > > > > } > > } > > return a.splice(0,jx); > > } > > That's clearer and more efficient. No, genius, no. > > Bubbles up the ones you need to delete - then cut off on the way > > back ... > > Shifts the set of good elements to the front and saves the length of > that set each iteration. No, it does not shift them it *duplicates* them, and in the process, it makes sparse arrays dense : a= []; a[10]= 27; for (p in a) console.log(p); --> 10 5 in p --> false for (p in removeThrees(a)) console.log(p); --> 0,1,2,3,4,5,6,7,8,9,10 5 in p --> true > (...) -- Jorge. |