Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A single javascript file, named automobile.js , which when run with node.js usin

ID: 3882195 • Letter: A

Question

A single javascript file, named automobile.js , which when run with node.js using the command "node automobile.js" produces the described results. You must make use of higher-order functions to sort the cars. You should not, for example, create entirely separate functions each with dedicated loops to sort the cars. You will need a loop (or potentially more than one loop depending on your sorting algorithm of choice) in the sortArr function but that is pretty much it. Use prototype whenever needed.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The correct answers are as follows:

The cars sorted by year are:          The cars sorted by make are:          The cars sorted by type are:

2010 Toyota Tacoma                      1990 Ford F-150                              2005 Lotus Elise Roadster

2008 Subaru Outback                     2000 GMC Tahoe                            2010 Toyota Tacoma Pickup

2005 Lotus Elise                             1995 Honda Accord                         1990 Ford F-150 Pickup

2000 GMC Tahoe                           2005 Lotus Elise                               2000 GMC Tahoe SUV

1995 Honda Accord                        2008 Subaru Outback                      2008 Subaru Outback Wagon

1990 Ford F-150                             2010 Toyota Tacoma                       1995 Honda Accord Sedan

Explanation / Answer

automobile.js

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

Automobile.prototype.logme = function(printType)
{
str = "(" + this.year + " " + this.make + " " + this.model;

if(printType)
str = str + " " + this.type;

str = str + ")";

console.log(str);
}
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 ){

for(i = 0 ; i < array.length; ++i)
{
minIdx = i;
for( j = i + 1; j < array.length; ++j)
{
if(comparator(array[j], array[minIdx]))
minIdx = j;
}

if(minIdx != i)
{
temp = array[i];
array[i] = array[minIdx];
array[minIdx] = temp;
}
}
}

/*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){
return auto1.year > auto2.year;
}

/*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){
return auto1.make > auto2.make;
}

/*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){
return auto1.type > auto2.type;
}

function printArray(array, flag){
for(i = 0 ; i < array.length; ++i)
array[i].logme(flag);
}

console.log("*****");

console.log("The cars sorted by year are:");
sortArr(yearComparator, automobiles);
printArray(automobiles, false);

console.log("The cars sorted by make are:");
sortArr(makeComparator, automobiles);
printArray(automobiles, false);

console.log("The cars sorted by type are:");
sortArr(typeComparator, automobiles);
printArray(automobiles, true);

console.log("*****");

=======================================================================================

output

*****
The cars sorted by year are:
(2010 Toyota Tacoma)
(2008 Subaru Outback)
(2005 Lotus Elise)
(2000 GMC Tahoe)
(1995 Honda Accord)
(1990 Ford F-150)
The cars sorted by make are:
(2010 Toyota Tacoma)
(2008 Subaru Outback)
(2005 Lotus Elise)
(1995 Honda Accord)
(2000 GMC Tahoe)
(1990 Ford F-150)
The cars sorted by type are:
(2008 Subaru Outback Wagon)
(1995 Honda Accord Sedan)
(2000 GMC Tahoe SUV)
(2005 Lotus Elise Roadster)
(2010 Toyota Tacoma Pickup)
(1990 Ford F-150 Pickup)

=======================================================================================

Feel free to reach out if you have any doubts.
Rate if the answer was helpful.
Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote