PDA

View Full Version : Help With Function



7079
07-07-2006, 06:18 PM
Hi,

I have been working on a function that is giving me a hard time. I have posted several times to this 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 the "Ford" selection 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 has been responding to my requests and this function is becomming frustrating without expert help.

BTW, someone mentioned in a previous post of mine 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:


[code]
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){
continue;
}
else{
md = myAutos[i].model + "|" + myAutos[i].model;
models3[++mdIndex] = md;
}
}
}

[code]

Thanks,

7079

Twey
07-07-2006, 07:49 PM
You've made a lot of changes to that, haven't you?
BTW, someone mentioned in a previous post of mine 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.Bad markup will cause problems with some scripts. It's inevitable. If you're having trouble with a script, the first thing you should do is use a correct DOM tree.

Please, please, please use
[/code] tags! It's very annoying to have to add the indentation back every damn time someone makes a change![code]function getModelsByMake(mType) {
var ta = [];
for(var i = 0; i<myAutos.length; ++i)
if(myAutos[i].make === mType)
ta.push(myAutos[i].model);
return ta;
}That will return an array of all the models of make mType. I think this is what you mean, but I'm still not entirely sure.

7079
07-07-2006, 08:06 PM
Twey,

I think the code you just posted is what I am looking for, thanks! I apologize for not including the [/code], I didn't realize that it worked like the HTML tags with opening and closing tag having a /, my apologies. I will work on this now and if I have any problems, I will post back to the forum with my success or failure.

Thanks,

7079

7079
07-07-2006, 08:18 PM
Twey,

Quick question, I am trying not to repeat duplicates, how can I do that all in the same function?

Thanks,

7079

Twey
07-07-2006, 08:41 PM
function getModelsByMake(mType) {
var ta = [];
for(var i = 0; i < myAutos.length; ++i)
if(myAutos[i].make === mType && ta.indexOf(myAutos[i].model) === -1)
ta.push(myAutos[i].model);
return ta;
}

7079
07-07-2006, 09:26 PM
Twey,

Unfortunately, the code you just posted is not returning results. Is there anything else on my end that I need to do to make it work?

Thanks,

7079

Twey
07-07-2006, 10:10 PM
The following code worked for me:
function Automobile(make, model) {
this.make = make;
this.model = model;
Automobile.all.push(this);
}

Automobile.all = [];

new Automobile("Ford", "Fiesta");
new Automobile("Ford", "Fiesta");
new Automobile("Peugot", "501");
new Automobile("Monkey", "Fiesta");

Automobile.getModelsByMake = function(make) {
var ta = [];
for(var i = 0; i < Automobile.all.length; ++i)
if(Automobile.all[i].make == make && ta.indexOf(Automobile.all[i].model) == -1)
ta[ta.length] = Automobile.all[i].model;
return ta;
}It's a simplified version of your script, you may need to tweak it a little for your needs.