-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw3.js
153 lines (135 loc) · 5.67 KB
/
hw3.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// You are not permitted to change this in any way
function Student(name, major, yearInSchool, club) {
this.name = name; // string, (e.g. "Jim", "Pam", "Michael")
this.major = major; // string, (e.g. "Computer Science", "Art", "Business")
this.yearInSchool = yearInSchool; // int, (e.g. 1, 2, 3, 4)
this.club = club; // string, (e.g. "Improv", "Art")
}
var students = [
new Student("Pam", "Art", 2, "Art"),
new Student("Michael", "Business", 4, "Improv"),
new Student("Dwight", "Horticulture", 1, "Karate"),
new Student("Jim", "Sports Science", 2, "Guitar"),
new Student("Angela", "Accounting", 4, "Cat"),
new Student("Toby", "Human Resources", 3, "Photography")
];
Student.prototype.logMe = function(clubPrint)
{
if (clubPrint) console.log(this.name + " - " + this.major + " - " + this.yearInSchool + " - " + this.club);
else console.log(this.name + " - " + this.major + " - " + this.yearInSchool);
}
/* 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, input)
{
//to mutate the original array, change the parameter input
//back to array, then comment out from here /*
var array = []
for (var i = 0; i < input.length; i++) {
array[i] = input[i];
}
//to here */
if (array.length < 2)
return array;
for (var rightmost = 1; rightmost < array.length; ++rightmost)
{
var leftpos = rightmost - 1;
while (leftpos >= 0)
{
if (comparator(array[leftpos], array[rightmost]))
--leftpos;
else
break;
}
if (leftpos < rightmost - 1)
{
var temp = array.splice(rightmost, 1);
array.splice(leftpos + 1, 0, temp[0]);
}
}
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 students are 'tied' according to the comparison rules then the order of
those 'tied' students is not specified and either can come first*/
/* This compares two students based on their year in school. Sort in descending order.*/
function yearComparator(student1, student2) {
return student1.yearInSchool < student2.yearInSchool;
}
/* This compares two students based on their major. It should be case insensitive and
makes which are alphabetically earlier in the alphabet are "greater" than ones that
come later (from A-Z).*/
function majorComparator(student1, student2) {
return student1.major.toUpperCase() > student2.major.toUpperCase();
}
/* This compares two students based on the club they're in. The ordering from "greatest"
to "least" is as follows: improv, cat, art, guitar, (types not otherwise listed).
It should be case insensitive. If two clubs are of equal type then the student who
has the higher year in school should be "greater."*/
function clubComparator(student1, student2) {
if (student1.club === student2.club)
return yearComparator(student1, student2)
else if (student1.club.toUpperCase() === "IMPROV") return false;
else if (student2.club.toUpperCase() === "IMPROV") return true;
else if (student1.club.toUpperCase() === "CAT") return false;
else if (student2.club.toUpperCase() === "CAT") return true
else if (student1.club.toUpperCase() === "ART") return false;
else if (student2.club.toUpperCase() === "ART") return true;
else if (student1.club.toUpperCase() === "GUITAR") return false;
else if (student2.club.toUpperCase() === "GUITAR") return true;
else return yearComparator(student1, student2);
}
/* Your program should output the following to the console.log, including each of the opening and closing
stars. All values in parenthesis should be replaced with appropriate values. To accomplish this, you will
have to sort the array of students using each comparator and then loop through the array and and call logMe
on each student of the now-sorted array. If the argument is 'true' then it prints the student's name, major,
year in school, and club affiliation. If the argument is 'false' then the club affiliation is ommited and
just the student's name, major and year in school is logged. Please carefully note which sorted results require
the club to be displayed and which do not.
**********
The students sorted by year in school are:
(Name - Major - Year) // of the "greatest" student
...
(Name - Major - Year) // of the "least" student
**********
The students sorted by major are:
(Name - Major - Year) // of the "greatest" student
...
(Name - Major - Year) // of the "least" student
**********
The students sorted by club affiliation are:
(Name - Major - Year - Club) // of the "greatest" student
...
(Name - Major - Year - Club) // of the "least" student
**********
As an example of what is expected to be printed to the console with logMe being sent True for a single student:
Jim - Sports Science - 2 - Guitar
*/
var yearOutput = sortArr(yearComparator, students);
console.log("The students sorted by year in school are:");
for (var i = 0; i < yearOutput.length; ++i)
{
yearOutput[i].logMe(false);
}
var majorOutput = sortArr(majorComparator, students);
console.log("The students sorted by major are:");
for (var i = 0; i < majorOutput.length; ++i)
{
majorOutput[i].logMe(false);
}
var clubOutput = sortArr(clubComparator, students);
console.log("The students sorted by club are:");
for (var i = 0; i < clubOutput.length; ++i)
{
clubOutput[i].logMe(true);
}