Results 1 to 8 of 8

Thread: Help With a Function

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

    Default Help With a Function

    I have the following function:

    <code>
    function setupmodels2() {
    var md = myAutos[0].model;
    models2[0] = md;
    mdIndex = 0;
    for(var i = 1; i < myAutos.length; i++) {
    if(myAutos[i].model == md){
    continue;
    }
    else{
    md = myAutos[i].model;
    models2[++mdIndex] = md;
    }
    }

    }
    </code>

    The function works fine, it retrieves all the models from the array objects. What I want to do is be more specific and retrieve myAutos[i].model based on the myAutos[i].make == "Ford". I have placed the test condition in several places within the function. I even initialized a variable outside the function making it global thinking it would help in some way, it didn't. I am new to JS and really need some help. I have invested lots of time and decided to come to the forum for help so I do not keep spinning my wheels for nothing.

    Many thanks in advance for your help!

    Thanks,

    7079

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

    Default

    Well, I'd write it like this:
    Code:
    function getModelsByMake(sMake) {
      var ret = [];
      for(var i=0;i<myAutos.length;i++)
        if(myAutos[i].make === sMake && ret.indexOf(myAutos[i].model) === -1)
          ret.push(myAutos[i].model);
      return ret;
    }
    
    var models2 = getModelsByMake("Ford");
    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,

    Thank you for taking the time to look at my question. The code that you provided is not working for some reason. I played around with it but couldn't get working. Here is the entire code:

    cars.js

    <code>
    //class is defined
    function automobile(make,model,year,miles){
    this.make = make || "N/A";
    this.model = model || "N/A";
    this.miles = miles || "N/A";
    this.year = year || null;
    this.showAuto = showAuto;
    }
    //method of automobile
    function showAuto(){
    for(var i = 0; i < myAutos.length; i++){
    myAutos[i] = "Make: " + this.make + "\n" +
    "Model: " + this.model + "\n" +
    "Miles: " + this.miles + "\n" +
    "Year: " + this.year + "\n";

    document.forms['myForm'].elements['results'].value += "\n" + myAutos[i];
    if(myAutos[i]==myAutos[i]){
    break;
    }
    }
    }

    //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);

    var makes = new Array(); //to be global scope

    function setupMakes() {
    var mk = myAutos[0].make;
    makes[0] = mk;
    mkIndex = 0;
    for( var i = 1; i < myAutos.length; i++ ) {
    if( myAutos[i].make == mk )
    continue;
    else {
    mk = myAutos[i].make;
    makes[++mkIndex] = mk;
    }
    }
    }

    var models = new Array(); //to be global scope

    function setupModels() {
    var md = myAutos[0].model;
    models[0] = md;
    mdIndex = 0;
    for(var i = 1; i < myAutos.length; i++) {
    if(myAutos[i].model == md){
    continue;
    }
    else{
    md = myAutos[i].model;
    models[++mdIndex] = md;
    }
    }
    }

    var years = new Array(); //to be global scope

    function setupYears() {
    var yr = myAutos[0].year;
    years[0] = yr;
    yrIndex = 0;
    for(var i = 1; i < myAutos.length; i++) {
    if(myAutos[i].year == yr){
    continue;
    }
    else{
    yr = myAutos[i].year;
    years[++yrIndex] = yr;
    }
    }
    }
    </code>

    cars_objects_ara.html

    <code>
    <html>
    <head>
    <title>Automobiles</title>
    <script src="cars.js" type="text/javascript"></script>
    <script type="text/javascript">
    <!--
    function getAuto(){
    for (var i = 0; i < myAutos.length; i++){
    if (myAutos[i].make == document.myForm.make.value){
    myAutos[i].showAuto();
    }
    }
    }
    //-->
    </script>
    </head>
    <body>
    <form name="myForm">
    Make: <select name="make" onChange="updatemodel(this.selectedIndex)" style="width: 150px" />
    <option value="SELECT A YEAR">SELECT A MAKE</option>
    <script type="text/javascript">
    <!--
    setupMakes();
    for( var i = 0; i < makes.length; i++ ) {
    document.write("<option value='" + makes[i] + "'>" + makes[i] + "</option>");
    }
    //-->
    </script>
    </select><br />

    Model:
    <select name="model" style="width: 150px" onClick="alert(this.options[this.options.selectedIndex].value)">
    </select><br />

    <input type="button" value="Search Vehicles" onClick="getAuto()" /><br />
    <textarea name="results" cols="40" rows="5"></textarea>
    </form>

    <script type="text/javascript">
    <!--
    var makelist=document.myForm.make
    var modellist=document.myForm.model
    var data_1=makes.sort().slice(0,1);
    var data_2=makes.sort().slice(1,2);
    var data_3=makes.sort().slice(2,3);


    var data_1_1=new Array();


    var models2 = new Array(); //to be global scope

    function setupmodels2() {
    var md = myAutos[0].model;
    models2[0] = md;
    mdIndex = 0;
    for(var i = 1; i < myAutos.length; i++) {
    if(myAutos[i].model == md){
    continue;
    }
    else{
    md = myAutos[i].model;
    models2[++mdIndex] = md;
    }
    }

    }

    document.write("<br>"+data_1+","+data_2+","+data_3);
    var model=new Array()
    model[0]=""




    model[1]=["New York|newyorkvalue", "Los Angeles|loangelesvalue", "Chicago|chicagovalue", "Houston|houstonvalue", "Austin|austinvalue"]
    model[2]=["Vancouver|vancouvervalue", "Tonronto|torontovalue", "Montreal|montrealvalue", "Calgary|calgaryvalue"]
    model[3]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue", "Birmingham|birminghamvalue"]
    model[4]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue", "Birmingham|birminghamvalue"]

    function updatemodel(selectedcitygroup){
    modellist.options.length=0
    if (selectedcitygroup>0){
    for (i=0; i<model[selectedcitygroup].length; i++)
    modellist.options[modellist.options.length]=new Option(model[selectedcitygroup][i].split("|")[0], model[selectedcitygroup][i].split("|")[1])
    }
    }
    //-->
    </script>

    </code>

    I posted the entire code hoping it will make helping me with this easier. I really appreciate your help and anyone else who may want to help.

    Thanks,

    7079

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

    Default

    The code that you provided is not working for some reason. I played around with it but couldn't get working. Here is the entire code:
    The error you get would be much more helpful. Also, the correct tag is [code], not <code>; this is vBulletin, not HTML.
    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!

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

    Default

    Hey,

    I forgot to mention that I am trying to create dynamic select boxes that change values based on what is selected in the previous box. I borrowed some code from another script on the internet that allowed dynamic select boxes and was trying to incorporate into this class project. That's why you see the arrays with the city names, I would lke to convert those so that they are dynamically populated with the content of the myAutos arrays.

    Thanks,

    7079

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

    Default

    Well, it's not giving me an error. It just doesn't do anything.

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

    Default

    Do you have a demo page?
    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!

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

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
  •