View Full Version : Help With a Function

07-07-2006, 08:33 PM

I have been working on a function that is giving me a hard time. I have posted several times to DynamicDrive forum and I am not receiving the help I need. Since the previous posts, I have made some changes and improvements to the function.

The getModelsByMake(mType) function should loop through the myAutos array objects and retrieve the models that are a specific type based on the (mType). It's 1/2 way working now and you can see the results for it at the following URL: http://7079.net/cars_objects_ara.html

It should only output "Crown Victoria|Crown Victoria, Taurus|Taurus", but it also is outputting everything else in the array object. If someone chooses "Nissan" as the parameter to pass to the function, they will receive the vehicles that are Nissan only.

I have output the 3 results for the 3 makes that I currently have in the array. If anyone can help me please, I would appreciate it so much. No one on the other forum has been responding to my requests and this function is becomming frustrating without expert help.

BTW, someone mentioned in a previous post on the other forum that the HTML is not to code etc.... The class that I am doing this for is a JS class and HTML is not being graded. I understand the importance of proper HTML, but for this, it is the function that is important.

Here is the function:

var models3 = new Array(); //to be global scope
function getModelsByMake(mType){
this.mType = mType;
var md = myAutos[0].model + "|" + myAutos[0].model;
models3[0] = md;
mdIndex = 0;
for(var i = 0;i<myAutos.length;i++){
if((myAutos[i].model + "|" + myAutos[i].model) === md && myAutos[i].make === mType){
md = myAutos[i].model + "|" + myAutos[i].model;
models3[++mdIndex] = md;


Thank you so much in advance!


07-07-2006, 08:49 PM
I think the reason you aren't getting good responses is because your question is quite confusing.

You've got 2 arrays: models3 and myAutos.

You talk about the output being incorrect but your function does not do any output, so clearly the function is attempting to do something that will ultimately be outputted, but what exactly is that?

Your example has a drop down menu with city names in them, but we don't see anything in the code that takes that into account. Does it have any relevance to your question?

Try posting more of your code, put them in ]code[ tags to make them easier for us to read, and be more specific.

I'd like to help, I just can't figure your post out in a reasonable time.

07-07-2006, 09:03 PM
Excellent point. The cities are a place holder for now, I am combining my function with a script for dynamic select boxes. That script happened to be about cities, etc...

myAutos is the array object containing all the vehicles and their information. models3 is the new array with the extracted results from the myAutos array. Is that better? I apologize for not mentioning that before.

I will take the models3 once it is populated with the correct data and add it to the model array in the appropriate place. For now, I need to get the getModelsByMake(mType) function working.

Can you please help? Is there anything else that I did not clarify or provide in order to receive the proper help?

Thank you so much for replying and I look forward to your help!


07-07-2006, 09:10 PM
I will post updated code on the link above

07-07-2006, 09:38 PM
A few clarifications please.

It seems there are objects being stored in the myAutos array and that they have two variables, make and model. Is this true?

And now, the goal is to get values out of myAutos with a make equivalent to the make passed into the function, and the problem is that values in myAutos with makes other than the make passed into the function are getting into models3?

Running on the assumption, it would seem this code would suffice:

var g_myAutos = new Array(); // *** I prefix my globals with g_
g_myAutos[0] = {'make':'x','model':1};
g_myAutos[1] = {'make':'x','model':2};
g_myAutos[2] = {'make':'x','model':3};
g_myAutos[3] = {'make':'y','model':1};
g_myAutos[4] = {'make':'y','model':2};
g_myAutos[5] = {'make':'y','model':3};

function getAutosByMake(p_make) // *** I prefix my parameters with p_
var returnArray = new Array();
for (var i = g_myAutos.length - 1; i >=0; --i)
if (g_myAutos[i].make == p_make)

return returnArray;

I suggest returning an array instead of setting a global like models3 because there are less side-effects of the function, in fact the function has no side-effects, and side-effects can cause confusion in complicated programs. By returning the array, if you want to store in a global variable you can, but if you only need to use it once, or want to assign it to a local variable, it's much cleaner this way.

Also, I'm not sure why you concatenated the same value to either end of pipe ( | ) character and then compared against the same value as well as against the make value. That doesn't seem to be useful. However, it does seem like you might be trying to avoid duplicates, assuming that the myAutos array will have dupes. Is this true?

Hope this helps.

07-07-2006, 09:52 PM
The assignment has to do with array objects, like below. The way you setup your g_myAutos array is different than the way I had set mine up. The ( | ) concatenation is for the dynamic select box. The code that I am using for dynamically populating the select boxes based on the previous selection are key|value pairs, or I assumed that when I first looked at the script. That's why I am doing what I am doing so that I can have the values in the model array for each make.

var myAutos = new Array();

myAutos[0] = new automobile("Ford","Crown Victoria",1999,156789);
myAutos[1] = new automobile("Ford","Crown Victoria",2000,89652);
myAutos[2] = new automobile("Ford","Crown Victoria",2001,22354);
myAutos[3] = new automobile("Ford","Crown Victoria",2002,105236);
myAutos[4] = new automobile("Ford","Crown Victoria",2003,12457);
myAutos[5] = new automobile("Ford","Crown Victoria",1999,36201);
myAutos[6] = new automobile("Ford","Taurus",2002,64512);
myAutos[7] = new automobile("Ford","Taurus",1999,126789);
myAutos[8] = new automobile("Ford","Taurus",2000,45652);
myAutos[9] = new automobile("Ford","Taurus",2001,37354);
myAutos[10] = new automobile("Ford","Taurus",2002,125236);
myAutos[11] = new automobile("Ford","Taurus",2003,42457);
myAutos[12] = new automobile("Ford","Taurus",1999,28201);
myAutos[13] = new automobile("Ford","Taurus",2002,54512);
myAutos[14] = new automobile("Nissan","Pathfinder",1999,156789);
myAutos[15] = new automobile("Nissan","Pathfinder",2000,89652);
myAutos[16] = new automobile("Nissan","Pathfinder",2001,22354);
myAutos[17] = new automobile("Nissan","Pathfinder",2002,105236);
myAutos[18] = new automobile("Nissan","Pathfinder",2003,12457);
myAutos[19] = new automobile("Nissan","Pathfinder",1999,36201);
myAutos[20] = new automobile("Nissan","Frontier",2002,64512);
myAutos[21] = new automobile("Nissan","Frontier",1999,126789);
myAutos[22] = new automobile("Nissan","Frontier",2000,45652);
myAutos[23] = new automobile("Nissan","Frontier",2001,37354);
myAutos[24] = new automobile("Nissan","Frontier",2002,125236);
myAutos[25] = new automobile("Nissan","Frontier",2003,42457);
myAutos[26] = new automobile("Nissan","Frontier",1999,28201);
myAutos[27] = new automobile("Nissan","Frontier",2002,54512);
myAutos[28] = new automobile("Toyota","Tacoma",2002,37912);

I appreciate the time you took to look at this and I would like your help again if you don't mind. The point about returning an array sounds good and I would like to do that based on your recommendation. Can you help me achieve this and to finish this script?



07-07-2006, 11:06 PM
You need to end your code tags with /code to get it to show up correctly.

anyway, what's wrong with what I gave you. You can modify it ever so slightly by changing the global auto array's name, use your array population code with the automobile object, and see what happens.

I looked at your page, and it seems like you're getting the output you want, you're listing all of the models by their make. Am I missing where the problem is?

07-07-2006, 11:29 PM
Sorry again for forgetting the / in the tag. You seen the page, the results for each array are duplicates, I want to remove the duplicates. Any suggestions?

Here is the function:

function getModelsByMake(mType) {
var ta = [];
for(var i = 0; i<myAutos.length; ++i)
if(myAutos[i].make === mType){
return ta;

I don't need "Crown Victoria|Crown Victoria" to repeat each time for each array object, just need it once and I can't seem to get that.



07-07-2006, 11:44 PM
hash table. Instead of an array, use a hash table (associative array)

function getModelsByMake(mType) {
var ta = {};
for(var i = 0; i<myAutos.length; ++i)
if(myAutos[i].make === mType && typeof ta[myAutos[i].model] == "undefined"){
return ta;

07-08-2006, 12:00 AM
I'm gone from now till Monday, good luck!

07-08-2006, 12:27 AM
Thanks, Have a safe trip!