Results 1 to 7 of 7

Thread: Help With Function

  1. #1
    Join Date
    Jun 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Function

    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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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][/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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jun 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Jun 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey,

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

    Thanks,

    7079

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    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;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jun 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The following code worked for me:
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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