...

View Full Version : Help with basic arrays and funcitons



jdr
12-08-2011, 07:39 PM
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.




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

Philip M
12-08-2011, 08:21 PM
Something like this?


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

Old Pedant
12-08-2011, 08:44 PM
Slightly better, create an array of CAR objects.



<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>.

jdr
12-08-2011, 09:26 PM
Please delete thread

Old Pedant
12-08-2011, 09:48 PM
In my version:


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.

Old Pedant
12-08-2011, 09:50 PM
In Philip's version:


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.

jdr
12-08-2011, 10:19 PM
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?

Old Pedant
12-08-2011, 10:42 PM
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.

jdr
12-08-2011, 10:51 PM
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?

Old Pedant
12-08-2011, 11:09 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum