N
nick
for (var i=0; i in a || i < a.length)
oops, that should be:
for (var i=0; i in a || i < a.length
for (var i=0; i in a || i < a.length)
Ry said:Ry said:function removeThrees (a) {
var i= 0;
while (i < a.length) a === 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.
nick said:[...]
Ry said:[...]
function removeThrees (a) {
var i= 0;
while (i < a.length) a === 3 ? a.splice(i, 1) : i++;
return a;
}
Terrible. Don't use the ternary operator in that fashion.
Why not?
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===3) a.splice(i, 1); else i++;
return a;
}
For dense arrays, this will also work:
function removeThrees (a) {
var i=0, e;
while (typeof (e=a)!='undefined')
if (e===3) a.splice(i, 1); else i++;
return a;
}
Ry said:Ry said:Ry Nohryb wrote:
function removeThrees (a) {
var i= 0;
while (i < a.length) a === 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 ?
Ry said:Ry Nohryb wrote:
function removeThrees (a) {
var i= 0;
while (i < a.length) a === 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.
Ry said:Ry Nohryb wrote:
Ry Nohryb wrote:
function removeThrees (a) {
var i= 0;
while (i < a.length) a === 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.
Ry said:Ry said:Ry Nohryb wrote:
Ry Nohryb wrote:
function removeThrees (a) {
var i= 0;
while (i < a.length) a === 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.
...and just look at the difference!
RobG said:Another try:
function removeThrees (a) {
var i=0;
while (i in a) {
if (a === 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 != 3) {
b.push(a);
} 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;
b = (t == 3)? o : t;
}
return b;
}
Another try:function removeThrees (a) {
var i=0;
while (i in a) {
if (a === 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 said:On 09/05/10 21:44, Garrett Smith wrote:
Another try:
function removeThrees (a) {
var i=0;
while (i in a) {
if (a === 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.
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.
Garrett Smith wrote:
[...]
How about a forward loop using splice?function removeThrees(a) {
for(var i = 0; i < a.length; i++) {
if(a === 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.
MikeMainFrame said:Garrett Smith wrote:
[...]
How about a forward loop using splice?
function removeThrees(a) {
for(var i = 0; i < a.length; i++) {
if(a === 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)
Johannes said: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(',');
MikeMainFrame said:[...]Garrett Smith wrote:
I have followed your discussions and want to suggest this:
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)
MikeMainFrame said: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.
Shifts the set of good elements to the front and saves the length of
that set each iteration.
MikeMainFrame said: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.
Shifts the set of good elements to the front and saves the length of
that set each iteration.
Garrett said: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.
Soshnikov said:"Let there is an array. How to remove all elements with value 3 from
it?"
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.