PDA

View Full Version : Array Objects Question



7079
06-30-2006, 02:18 PM
Below is the code for a class project I am working on which I am stuck on. I have tried several things to resolve the issue, but I have already spent too much time to trying to solve on my own. If anyone out there can help me, I will greatly appreciate it as I am new to Javascript.

Page the user will see:

<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" />
<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" />
<option value="SELECT A YEAR">SELECT A MODEL</option>
<script type="text/javascript">
<!--
setupModels();
for( var i = 0; i < models.length; i++ ) {
document.write("<option value='" + models[i] + "'>" + models[i] + "</option>");
}
//-->
</script>
</select><br />
Year: <select name="year" />
<option value="SELECT A YEAR">SELECT A YEAR</option>
<script type="text/javascript">
<!--
setupYears();
for( var i = 0; i < years.length; i++ ) {
document.write("<option value='" + years[i] + "'>" + years[i] + "</option>");
}
//-->
</script>
</select><br />
Miles: <input type="text" name="miles" /><br />
<input type="button" value="Search Vehicles" onclick="getAuto()" /><br />
<textarea name="results" cols="40" rows="5"></textarea>
</form>
<script type="text/javascript">
<!--
//-->
</script>
</body>
</html>

.js file contents below:

//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] = document.myForm.results.value =
"Make: " + this.make + "\n" +
"Model: " + this.model + "\n" +
"Miles: " + this.miles + "\n" +
"Year: " + this.year;
}
}

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

Right now only the first drop down is working for testing purposes. I would like when someone selects "Ford" and clicks the show vehicles button, the entire list of "Ford" vehicles should be output to the textarea box. Right now, only either the first or last will display. I am not doing something right with my code.

If you need further information in order to help me, please advise and I will provide the necessary information. I think the code above should be it since it is all the code for this small JS app.

Thanks,

7079

Twey
06-30-2006, 02:23 PM
Please put the code in
tags.

Anyway, the most obvious problem is here:[code]myAutos[i] = document.myForm.results.value =
"Make: " + this.make + "\n" +
"Model: " + this.model + "\n" +
"Miles: " + this.miles + "\n" +
"Year: " + this.year;You're overwriting any other contents of the variables. You should do:
myAutos[i] = "Make: " + this.make + "\n" +
"Model: " + this.model + "\n" +
"Miles: " + this.miles + "\n" +
"Year: " + this.year;

document.forms['myForm'].elements['results'].value += "\n" + myAutos[i];

7079
06-30-2006, 03:08 PM
Twey,

Thank you so much for the quick response! I made the change and it works, the only thing is that it repeats the first result item for the length of the array. It's not showing the other "Ford" items. I played with it by making some changes, I am not making the right changes. Please let me know if you have any suggestions.

BTW, my aplogies for not surrounding the code with the <code> tags, I will make sure not to let that happen again.

Thanks,

7079

7079
06-30-2006, 03:13 PM
I made a change to the following code but it is still repeating each array more than once rather than once each.

Here is the code:

<code>
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];

}
}
</code>