Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help With a Function

    Hi,

    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:


    [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]

    Thank you so much in advance!

    7079

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

    7079

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I will post updated code on the link above

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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:
    Code:
    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)
          {
             returnArray.push(g_myAutos[i]);
          }
      }
    
      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.

  • #6
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

    [code]
    //inventory
    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);
    [code]

    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?

    Thanks,

    7079

  • #7
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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?

  • #8
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:

    Code:
    function getModelsByMake(mType) {
      var ta = [];
      for(var i = 0; i<myAutos.length; ++i)
        if(myAutos[i].make === mType){
          ta.push(myAutos[i].model+"|"+myAutos[i].model);  
    		}
    		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.

    Thanks,

    7079

  • #9
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    hash table. Instead of an array, use a hash table (associative array)

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

  • #10
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    I'm gone from now till Monday, good luck!

  • #11
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, Have a safe trip!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •