-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
187 lines (168 loc) · 6.31 KB
/
app.js
File metadata and controls
187 lines (168 loc) · 6.31 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var first, mid, last, val, tileVals;
var elems = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
// var elemsLarge = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
var chip1 = '<span class="mdl-chip mdl-chip--contact"><span class="mdl-chip__contact mdl-color--Indigo mdl-color-text--white">';
var chip1DO = '<span class="mdl-chip mdl-chip--contact"><span class="mdl-chip__contact mdl-color--Deep-Orange mdl-color-text--white">';
var chip2 = '</span><span class="mdl-chip__text">';
var chip3 = '</span></span>';
$(function () {
initalSetup(elems);
main();
// Functions for buttons
$(".reset").on("click", function () {
$(".tiles").children().remove();
$(".mdl-textfield__input").val("");
initalSetup(elems);
main();
enableStepStart();
});
$(".start").on("click", function () {
val = $(".mdl-textfield__input").val().toUpperCase();
setIndexColors(first, mid, last);
disableStart();
});
// TODO: MISSING A CHECK IN THIS!!!!!!
$(".step").on("click", function () {
var midVal, firstIndex, lastIndex;
if (!$(".start").hasClass("hidden")) {
alert("Cannot step without starting first!");
return;
}
if (val != "") {
midVal = $(mid).text();
firstIndex = tileVals.indexOf(first);
lastIndex = tileVals.indexOf(last);
if (first > last)
clearIndexColors(first, mid, last);
$(".mdl-textfield__input").text(val + " not in array.");
if (midVal === val) {
clearIndexColors(first, mid, last);
setFoundChip(mid, val);
} else if (midVal > val){
clearIndexColors(first, mid, last);
last = tileVals[tileVals.indexOf(mid) - 1];
mid = getMidTile(firstIndex, tileVals.indexOf(last));
if (tileVals.indexOf(last) < 0) {
notInArray(first, mid, last, val);
} else {
setIndexColors(first, mid, last);
}
} else {
clearIndexColors(first, mid, last);
first = tileVals[tileVals.indexOf(mid) + 1];
mid = getMidTile(tileVals.indexOf(first), lastIndex);
if (tileVals.indexOf(first) < 0) {
notInArray(first, mid, last, val);
} else {
setIndexColors(first, mid, last);
}
}
} else {
alert("Search cannot be empty. Please press reset, and supply a letter to search.");
}
});
function getMidTile(first, last) {
return tileVals[Math.floor((first + last) / 2)]
}
function notInArray (first, mid, last, val) {
clearIndexColors(first, mid, last);
$(".mdl-textfield__input").val(val + " not in array.");
disableStep();
}
function disableStep () {
$(".step").addClass("hidden");
$(".step-disabled").removeClass("hidden");
}
function disableStart () {
$(".start").addClass("hidden");
$(".start-disabled").removeClass("hidden");
}
function enableStepStart () {
$(".start").removeClass("hidden");
$(".start-disabled").addClass("hidden");
$(".step").removeClass("hidden");
$(".step-disabled").addClass("hidden");
}
function main () {
tileVals = $(".mdl-card__title-text").toArray();
first = tileVals[0];
last = tileVals[tileVals.length - 1];
mid = getMidTile(0, tileVals.length - 1);
}
/**
* Addes chips to the first, mid, and last cards
*/
function setIndexColors (first, mid, last) {
var f, m, l, currF, currM, currL;
f = $(first).parent().next();
l = $(last).parent().next();
m = $(mid).parent().next();
currF = f.text();
currM = m.text();
currL = l.text();
if (currF == currM && currM == currL) {
f.html(chip1 + currF + chip2 + "ALL" + chip3);
} else if (currM == currL) {
f.html(chip1 + currF + chip2 + "FIRST" + chip3);
l.html(chip1 + currL + chip2 + "M / L" + chip3);
} else if (currF == currM) {
f.html(chip1 + currF + chip2 + "F / M" + chip3);
l.html(chip1 + currL + chip2 + "LAST" + chip3);
} else {
f.html(chip1 + currF + chip2 + "FIRST" + chip3);
l.html(chip1 + currL + chip2 + "LAST" + chip3);
m.html(chip1 + currM + chip2 + "MID" + chip3);
}
}
function setFoundChip (tile, val) {
var t, curr;
t = $(tile).parent().next();
curr = t.text();
t.html(chip1DO + curr + chip2 + "FOUND" + chip3);
disableStep();
$(".mdl-textfield__input").val(val + " found at index: " + tileVals.indexOf(tile));
}
/**
* Removes chips from the first, mid, and last cards
*/
function clearIndexColors (first, mid, last) {
var f, m, l, curr;
// First
f = $(first).parent().next();
curr = f.children().text();
f.html(curr[0]);
// Mid
m = $(mid).parent().next();
curr = m.children().text();
m.html(curr[0]);
// Last
l = $(last).parent().next();
curr = l.children().text();
l.html(curr[0]);
}
/**
* Returns the available tiles as an
* array of associated char values.
*/
function getTileArray () {
var tileArray = $(".mdl-card__title-text").toArray();
var result = [];
tileArray.forEach(function (item) {
result.push($(item).text().charCodeAt(0));
});
return result;
}
/**
* Sets up the demo.
*/
function initalSetup (arr) {
var tilePt1 = "<div class=\"demo-card-square mdl-card mdl-shadow--2dp\"><div class=\"mdl-card__title mdl-card--expand\"><h2 class=\"mdl-card__title-text\">";
var tilePt2 = "</h2></div><div class=\"mdl-card__supporting-text\">";
var tilePt3 = "</div></div>";
var tiles = $(".tiles");
for (var index = 0; index < arr.length; index++) {
var completedTile = tilePt1 + arr[index] + tilePt2 + index + tilePt3;
$(completedTile).appendTo(tiles);
}
}
});