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 10 of 10
  1. #1
    jdr
    jdr is offline
    New Coder
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help with basic arrays and funcitons

    Hi, i need to ask the user for details about a car, i will not know how many cars are going to be stored and therefore need an array i think. I need the function for asking the user and will need to repeat the code later in a menu. Can somebody help me please? at this stage i just want to ask the user for input, store it in array and use an alert to check if it works? Thanks.

    Code:
     
    
    <html>
    <script>
    
    
    // Purpose: Gather car information and store it in a datbase
    
    
    // Ask user for REG info, CAR MAKE, car VIN
    
    var CarDetails = {
    RegNum:"",CarMake:"", CarVin:"";
    }
    
    function enterRegNum() {
    prompt("Enter Reg number");
    }
    
    function enterCarMake() {
    prompt("Enter car make");
    
    function enterCarVin() {
    prompt("Enter Car Vin number")
    }
    
    
    
    </script>
    </html>
    Last edited by Apostropartheid; 12-10-2011 at 05:35 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Something like this?

    Code:
    <script type = "text/javascript">
    
    // Purpose: Gather car information and store it in a datbase
    
    // Ask user for REG info, CAR MAKE, car VIN
    
    var howMany = prompt("Enter details for how many cars?", "");
    
    var CarDetails = new Array(howMany);
    for (var i = 1; i <= howMany; i++) {	
    CarDetails[i] = [' ', ' ', ' '];
    }
    
    for (var i = 1; i <=howMany; i++) {
    CarDetails[i][0] = prompt ("Enter Registration Number for car No." + i,"");
    CarDetails[i][1] = prompt ("Enter Car Make for car No." + i,"");
    CarDetails[i][2] = prompt ("Enter Car VIN Number for car No." + i,"");
    }
    
    alert (CarDetails);
    
    </script>
    Obviously you will need to validate the entries, but this will give you a start.

    “What is a cynic? A man who knows the price of everything and the value of nothing.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,016
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Slightly better, create an array of CAR objects.

    Code:
    <script type = "text/javascript">
    // constructor for a Car ojbect:
    function Car( reg, make, vin )
    {
        this.registration = reg;
        this.manufacturer = make;
        this.VIN = vin;
        this.toString = function() {
               return "\nregistraion: " + this.registration 
                    + ", make: " + this.manufacturer 
                    + ", vin: " + this.VIN; };
    }
    
    // Purpose: Gather car information and store it in a datbase
    // Ask user for REG info, CAR MAKE, car VIN
    var howMany = prompt("Enter details for how many cars?", "");
    
    var cars = [];
    
    for (var i = 1; i <=howMany; i++) {
         var r = prompt ("Enter Registration Number for car No." + i,"");
         var m = prompt ("Enter Car Make for car No." + i,"");
         var v = prompt ("Enter Car VIN Number for car No." + i,"");
         cars.push( new Car( r, m, v ) );
    }
    
    alert( cars );
    </script>
    Not suggesting you turn this in for your homework. Just trying to head you in the right future directions.

    In reality, you would never use prompt for any real-world web application. You'd create a <form>, possibly with multiple rows, and have the user fill in the <form>.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    jdr
    jdr is offline
    New Coder
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Please delete thread
    Last edited by jdr; 12-10-2011 at 03:48 PM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,016
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    In my version:
    Code:
    function findCarByMake( make )
    {
        var results;
        for ( var c = 0; c < cars.length; ++c )
        {
            var car = cars[c];
            if ( car.manufacturer == make ) { results.push(car); }
        }
        return results; // returns an array of Car objects!
    }
    Notice that it returns an array, in case there is more than one car with the given make. The array will be empty if there are no matches.

    You actually can write a generic function that lets you specify which field to find by, but with only 3 fields I'd just create 3 functions.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,016
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    In Philip's version:
    Code:
    function findCarByMake( make )
    {
        var results;
        for ( var c = 0; c < CarDetails.length; ++c )
        {
            var car = CarDetails[c];
            if ( car[1] == make ) { results.push(car); }
        }
        return results; // returns an array of Car objects!
    }
    As you can see, you need to *know* that the make is element [1] of the inner arrays. It works, but it's not as self-documenting.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    jdr
    jdr is offline
    New Coder
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you this has been very useful, how would i search through the cars i have saved. For example query by car number? would i use a function to search for an index?
    Last edited by Apostropartheid; 12-10-2011 at 05:35 PM.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,016
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Why not try it?

    But yes. Isn't that what I said?

    Notice that it returns an array, in case there is more than one car with the given make.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    jdr
    jdr is offline
    New Coder
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes i did check got it working thanks, but what about the indexes from the array, if i wanted to search a car by its number saved, if i saved 10 cars, the first should have the index [0] if i am right. Can i get the indexes even if i haven't declared them in the array because i cant specify how many cars i will enter?
    Last edited by Apostropartheid; 12-10-2011 at 05:36 PM.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,016
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Again, why didn't you try it?

    Yes, of course. The whole point of an array is being able to get one element of it via the index into the array.

    If you have 10 elements in the array, they are numbered 0 through 9.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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