What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 127 Stack Exchange communities.

I recently (~1hr ago) answered a question on SO about dynamic variables in javascript. Basically, the asker wanted to do something like this:

var i = 0;
while ( i < arr.length )
{ 
    var "varName" + i = new SomethingOrOther();
    i++;
}

Naturally, that's not valid javascript and would throw an error. Two answers were already present indicating that the OP should create an object associative-array style and set the properties dynamically using []:

var myObj = {};
var i = 0;
while ( i < arr.length )
{ 
    myObj["varName" + i] = new SomethingOrOther();
    i++;
}

This is all well and good, it answers the question but IMO it's horrendous advice. I didn't downvote because I didn't want to seem too pushy, the answers did answer the question after all. But in reality, this is terrible JavaScript and arrays were built for this purpose:

var myArr = [];
var i = 0;
while ( i < arr.length )
{ 
    myArr.push(new SomethingOrOther());
    i++;
}

Which is pretty much a summary of my answer. My answer received minimal votes and the OP ended up going with one of the other answers, on to write "bad javascript".

Which leads me to my question - do you think it's better to answer the question or actually try and point people in a better direction if one is available? What would get your vote?

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

It's always better to give a good answer. You can also make comments to let people know what their answers are missing.

share|improve this answer
    
That's what I usually do. And I would vote for answers that try and provide a better alternative solution to the problem. I guess it's just frustrating when you're trying to help someone and they just ignore it because taking the easy way out is, well, easier. –  Andy E Mar 10 '10 at 13:48
add comment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .