-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomobile.js
125 lines (107 loc) · 5.19 KB
/
automobile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function Automobile( year, make, model, type ){
this.year = year; //integer (ex. 2001, 1995)
this.make = make; //string (ex. Honda, Ford)
this.model = model; //string (ex. Accord, Focus)
this.type = type; //string (ex. Pickup, SUV)
}
var automobiles = [
new Automobile(1995, "Honda", "Accord", "Sedan"),
new Automobile(1990, "Ford", "F-150", "Pickup"),
new Automobile(2000, "GMC", "Tahoe", "SUV"),
new Automobile(2010, "Toyota", "Tacoma", "Pickup"),
new Automobile(2005, "Lotus", "Elise", "Roadster"),
new Automobile(2008, "Subaru", "Outback", "Wagon")
];
/*This function sorts arrays using an arbitrary comparator. You pass it a comparator and an array of objects appropriate for that comparator and it will return a new array which is sorted with the largest object in index 0 and the smallest in the last index*/
function sortArr( comparator, array ){
/*using a basic bubble sort algorithm for simplicity's sake*/
for(var i=0; i<array.length-1; i++){
for(var j=i+1; j<array.length; j++){
if(!(comparator(array[i], array[j]))){ //comparitors will return false if a swap needs to occur
var temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
return array;
}
/*A comparator takes two arguments and uses some algorithm to compare them. If the first argument is larger or greater than the 2nd it returns true, otherwise it returns false. Here is an example that works on integers*/
function exComparator(int1, int2){
if (int1 > int2){
return true;
} else {
return false;
}
}
/*For all comparators if cars are 'tied' according to the comparison rules then the order of those 'tied' cars is not specified and either can come first*/
/*This compares two automobiles based on their year. Newer cars are "greater" than older cars.*/
function yearComparator( auto1, auto2 ){
if(auto1.year<=auto2.year) return true; //if auto1 has a year less than (or matches year of) auto two, no swap needs occur
else return false;
}
/*This compares two automobiles based on their make. It should be case insensitive and makes which are alphabetically earlier in the alphabet are "greater" than ones that come later.*/
function makeComparator(auto1, auto2){
if(auto1.make.toLowerCase()<=auto2.make.toLowerCase()) return true; //convert to lowercase for comparison to ensure case insensitivity
else return false;
}
/*This compares two automobiles based on their type. The ordering from "greatest" to "least" is as follows: roadster, pickup, suv, wagon, (types not otherwise listed). It should be case insensitive. If two cars are of equal type then the newest one by model year should be considered "greater".*/
function typeComparator(auto1, auto2){
var getType=function(auto){
switch(auto.type.toLowerCase()){
case "roadster":
return 0;
break;
case "pickup":
return 1;
break;
case "suv":
return 2;
break;
case "wagon":
return 3;
break;
default:
return 4;
break;
}
};
var auto1type, auto2type; //will convert types to numbers, to aid in sorting.
auto1type=getType(auto1);
auto2type=getType(auto2);
if(auto1type<auto2type) return true; //if autotype1 is lower than autotype 2, return no need to sort
else if(auto1type==auto2type) return yearComparator(auto1, auto2) //if types are the same, return results of year compare
else return false; //time to switch
}
/*Your program should output the following to the console.log, including the opening and closing 5 stars. All values in parenthesis should be replaced with appropriate values. Each line is a seperate call to console.log.
Each line representing a car should be produced via a logMe function. This function should be added to the Automobile class and accept a single boolean argument. If the argument is 'true' then it prints "year make model type" with the year, make, model and type being the values appropriate for the automobile. If the argument is 'false' then the type is ommited and just the "year make model" is logged.
*****
The cars sorted by year are:
(year make model of the 'greatest' car)
(...)
(year make model of the 'least' car)
The cars sorted by make are:
(year make model of the 'greatest' car)
(...)
(year make model of the 'least' car)
The cars sorted by type are:
(year make model type of the 'greatest' car)
(...)
(year make model type of the 'least' car)
*****
As an example of the content in the parenthesis:
1990 Ford F-150 */
Automobile.prototype.logMe=function(validation){
var myString=this.year + ' ' + this.make + ' ' + this.model; //base string
if(validation) myString+=' '+ this.type; //if true boolean is passed, add type
console.log(myString); //print the string
}
console.log("The cars sorted by year are:");
var yearSort=sortArr(yearComparator, automobiles);
for(var i=0; i<yearSort.length; i++) yearSort[i].logMe(false); //forEach was buggy for me. This will be fixed (I hope)
console.log("The cars sorted by make are:");
var makeSort=sortArr(makeComparator, automobiles);
for(var i=0; i<makeSort.length; i++) makeSort[i].logMe(false);
console.log("The cars sorted by type are:");
var typeSort=sortArr(typeComparator, automobiles);
for(var i=0; i<typeSort.length; i++) typeSort[i].logMe(true);