From: Stefan Weiss on 10 May 2010 22:36 On 11/05/10 03:12, 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? Yes, the problem is underspecified. That may be a good thing in an interview situation - can the applicant see the problem? Does he know what a sparse array is? Does he dare to question the requirements? How will he communicate his dilemma? The problem itself is so simple that I would guess Dmitry's example was more geared at this meta level. I find it amusing that such a primitive task would be taken seriously by so many people in this group, and would bring forward so many different attempts at a solution. I guess most of us just love challenges :) In any case, one thing is certain: Dmitry wanted all threes removed (whatever that means for the resulting array), and a function which leaves a three in cannot be correct. -- stefan
From: David Mark on 11 May 2010 00:30 Stefan Weiss wrote: > On 11/05/10 03:12, 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? > > Yes, the problem is underspecified. That may be a good thing in an > interview situation - can the applicant see the problem? Does he know > what a sparse array is? Does he dare to question the requirements? How > will he communicate his dilemma? The problem itself is so simple that I > would guess Dmitry's example was more geared at this meta level. I find > it amusing that such a primitive task would be taken seriously by so > many people in this group, and would bring forward so many different > attempts at a solution. I guess most of us just love challenges :) Attempts? Ambiguities aside, I stand by my solution. > > In any case, one thing is certain: Dmitry wanted all threes removed > (whatever that means for the resulting array), and a function which > leaves a three in cannot be correct. > > That's for sure. :)
From: Dmitry A. Soshnikov on 11 May 2010 04:03 On 11.05.2010 6:36, Stefan Weiss wrote: > On 11/05/10 03:12, RobG wrote: [...] >> >> 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? > > Yes, the problem is underspecified. That may be a good thing in an > interview situation - can the applicant see the problem? Does he know > what a sparse array is? Does he dare to question the requirements? How > will he communicate his dilemma? The problem itself is so simple that I > would guess Dmitry's example was more geared at this meta level. > Yes, absolutely correct; and that's the main goal. The exact correct solution even is not so essential as essential the way of thinking, understanding and explanations provided by the applicant. By such simple small tasks we can test _how_ an employee will solve the suggested issue, what questions will he ask (and if will, if which form and so on), and he's questions/suggestions can help to analyze his knowledge level completely. E.g. he can only mention concept of sparse arrays (but not to think how to apply this case also in the solutions) -- and it will be enough. Etc. Dmitry.
From: MikeMainFrame on 14 May 2010 05:40 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. I have followed your discussions and want to suggest this: WScript.echo(removeThrees([1,2,3,4,5,6,7,8])); 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); } Bubbles up the ones you need to delete - then cut off on the way back ... I use to do mainframe programming and I was surprised the first time I experienced the "living" length property - not used to OO ;o) 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)
From: David Mark on 14 May 2010 06:00
MikeMainFrame 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. > > I have followed your discussions and want to suggest this: > > WScript.echo(removeThrees([1,2,3,4,5,6,7,8])); > > 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); > } > > Bubbles up the ones you need to delete - then cut off on the way > back ... Interesting take. Definitely many ways to skin this cat. > > I use to do mainframe programming and I was surprised the first time I > experienced the "living" length property - not used to OO ;o) It's more of an ECMAScript thing than OO. And realize that in your rendition the length property is unnecessarily evaluated on every iteration. You are missing a semi-colon too. > > I was looking for some critisism on JQuery and the like. You shouldn't have to look very far around here. It takes a long time to permeate to the mainstream though as many seem to see such (valid) criticism as an indictment of open source in general (which it is not). > Found this - > and how nice it was to learn that a professional has to know the > concepts of the language ;o) What a revelation. If only the library developers would take this to heart. I'm sure some of them have learned over the years, but the "major" efforts are trapped in their own initial, ill-advised designs due to overwhelming early adoption. |