-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalpha.js
More file actions
168 lines (115 loc) · 3.91 KB
/
Copy pathalpha.js
File metadata and controls
168 lines (115 loc) · 3.91 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
////////////////////////////////////////////////////
// alpha.js
///////////////////////////////////////////////////
var alphabet = "abcdefghijklmnopqrstuvwxyz";
names = ["amy kat", "berry jam", "dan ricky", "joe leaf", "joel kardi", "rik kart", "steve pete", "steve pratt", "zarg hat"];
jsonNames = jsonify(names);
alpha1 = indexIsGoesAfter(jsonNames);
document.getElementById("names").innerHTML = alpha1;
function jsonify(list) {
jsonList = [];
for (var i = 0; i < list.length; i++) {
var spaceIndex = list[i].indexOf(" ");
var first = list[i].slice(0, spaceIndex);
var last = list[i].slice(spaceIndex, list[i].length);
jsonList.push({ "first" : first , "last" : last });
}
return jsonList;
}
function goesBefore(newJsonName, oldJsonName) {
//returns true if new goes before old, false otherwise
var newLast = newJsonName.last;
var newFirst = newJsonName.first;
var oldLast = oldJsonName.last;
var oldFirst = oldJsonName.first;
for (var i = 0; i < Math.min(newLast.length, oldLast.lenght); i++) {
if (newLast == oldLast) {
break;
}
if (alphabet.indexOf(newLast[i]) > alphabet.indexOf(oldLast[i])) {
return false;
} else if (alphabet.indexOf(newLast[i]) < alphabet.indexOf(oldLast[i])) {
return true;
}
}
//same last names
for (var i = 0; i < Math.min(newFirst.length, oldFirst.lenght); i++) {
if (newFirst == oldFirst) {
//same name, doesnt matter
return false;
}
if (alphabet.indexOf(newLast[i]) > alphabet.indexOf(oldLast[i])) {
return false;
} else if (alphabet.indexOf(newLast[i]) < alphabet.indexOf(oldLast[i])) {
return true;
}
}
}
//alg 1
function indexIsGoesAfter(jsonList) {
//put each name at the index that is same num as how many names it is after
var alphaJsonList = jsonList;
for (var i = 0; i < jsonList.length; i++) {
var numGoesAfter = 0;
for (var j = 0; j < jsonList.length && j != i; i++) {
if (!goesBefore(jsonList[i], jsonList[j])) {
numGoesAfter += 1;
}
}
alphaJsonList[numGoesAfter] = jsonList[i];
}
return alphaJsonList;
}
// function numberize(letter) {
// for (var i = 0; i < alpha.length; i++)
// if (alpha[i].toLowerCase() === letter.toLowerCase()) {
// return i;
// }
// }
// function put_in_alpha(list, new_word) {
// var alpha_list = [];
// if (list === []) {
// alpha_list.push(new_word);
// return alpha_list;
// }
// for (var i = 0; i < list.length; i++) {
// for (var j = 0; j < list[i].length; j++) {
// if (list[i][j] == " " && new_word[j] == " ") { //same first name
// alert("hi");
// continue;
// } else if (list[i][j] == " " && new_word[j] != " ") { //same but new longer
// alpha_list.push(new_word);
// for (var k = i; k < list.length; k++) {
// alpha_list.push(list[k]);
// }
// return alpha_list;
// } else if (list[i][j] != " " && new_word[j] == " ") { //same but old longer
// alert("hi");
// alpha_list.push(new_word);
// for (var k = i; k < list.length; k++) {
// alpha_list.push(list[k]);
// }
// return alpha_list;
// }
// if (alphabet.indexOf(new_word[j]) < alphabet.indexOf(list[i][j])) {
// alpha_list.push(new_word);
// for (var k = i; k < list.length; k++) {
// alpha_list.push(list[k]);
// }
// return alpha_list;
// } else if (j + 1 > list[i].length) {
// alpha_list.push(list[i]);
// } else {
// alpha_list.push(list[i]);
// break;
// }
// }
// }
// }
// function alphabetize(list) {
// var final_alpha = [];
// for (var i = 0; i < list.length; i++) {
// final_alpha = put_in_alpha(final_alpha, list[i]);
// }
// return final_alpha;
// }