-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithm.js
218 lines (210 loc) · 5.97 KB
/
algorithm.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// create map of categories and their values
let vals = new Map();
let valuesArr = [0,0,0,0,0,0,0];
let category;
//setting all the modifiers as 0
let pandaVal = 0;
let catVal = 0;
let dogVal = 0;
let foxVal = 0;
let birdVal = 0;
let memesVal = 0;
let redpandaVal = 0;
let koalaVal = 0;
let jokesVal = 0;
let raccoonVal = 0;
let kangarooVal = 0;
let modBase = 2;
initiate();
function initiate(){
//setting values for all categories in map as 0
vals.set("panda",0);
vals.set("cat",0);
vals.set("dog",0);
vals.set("fox",0);
vals.set("bird",0);
vals.set("memes",0);
vals.set("jokes",0);
vals.set("redpanda",0);
vals.set("koala",0);
vals.set("raccoon",0);
vals.set("kangaroo",0);
}
//this function chooses the category for the next image
function chooseCategory(){
//First getting value of each category
pandaVal = vals.get("panda");
catVal = vals.get("cat");
dogVal = vals.get("dog");
foxVal = vals.get("fox");
birdVal = vals.get("bird");
memesVal = vals.get("memes");
jokesVal = vals.get("jokes");
redpandaVal = vals.get("redpanda");
koalaVal = vals.get("koala");
raccoonVal = vals.get("raccoon");
kangarooVal = vals.get("kangaroo");
//Rolling a value for each category between 1 and 20 and adding modifier
pandaRand = Math.floor(Math.random()*21) + pandaVal;
catRand = Math.floor(Math.random()*21) + catVal;
dogRand = Math.floor(Math.random()*21) + dogVal;
foxRand = Math.floor(Math.random()*21) + foxVal;
birdRand = Math.floor(Math.random()*21) + birdVal;
memesRand = Math.floor(Math.random()*21) + memesVal;
jokesRand = Math.floor(Math.random()*21) + jokesVal;
redpandaRand = Math.floor(Math.random()*21) + redpandaVal;
koalaRand = Math.floor(Math.random()*21) + koalaVal;
raccoonRand = Math.floor(Math.random()*21) + raccoonVal;
kangarooRand = Math.floor(Math.random()*21) + kangarooVal;
let choiceArr = [pandaRand, catRand, dogRand, foxRand, birdRand, memesRand, jokesRand, redpandaRand, koalaRand, raccoonRand, kangarooRand];
let sortedArr = [pandaRand, catRand, dogRand, foxRand, birdRand, memesRand, jokesRand, redpandaRand, koalaRand, raccoonRand, kangarooRand];
//sorting the array in reverse order, highest value(first term) will be the one chosen
sortedArr.sort(function(a, b){return b - a});
//creating array with just the values, and array with just the keys, same order as the map
let keysArr = [...vals.keys()]
//getting index of the chosen value from the array of just the values
loc = choiceArr.indexOf(sortedArr[0]);
//using index to get the key from the array of the keys
category = keysArr[loc];
return category;
}
//calls the appropriate function to generate the right image
function generateImage(img, info){
if(category=="panda"){
getPandaImage(img, info);
}
else if(category=="cat"){
getCatImage(img, info);
}
else if(category=="dog"){
getDogImage(img, info);
}
else if(category=="fox"){
getFoxImage(img, info);
}
else if(category=="bird"){
getBirdImage(img, info);
}
else if(category=="memes"){
getMemeImage(img, info);
}
else if(category=="jokes"){
getJokeImage(img, info);
}
else if(category=="redpanda"){
getRedPandaImage(img, info);
}
else if(category=="koala"){
getKoalaImage(img, info);
}
else if(category=="raccoon"){
getRaccoonImage(img, info);
}
else if(category=="kangaroo"){
getKangarooImage(img, info);
}
}
/*
function to call when user selects like, increases the modifier value of the corresponding category
Also added condition to keep the max modifier under 20 so that the user never sees exclusively one
category
*/
function likeImage(categoryParam){
if(categoryParam=="panda"){
if(pandaVal<=20){
pandaVal+=modBase;
}
}
else if(categoryParam=="cat"){
if(catVal<=20){
catVal+=modBase;
}
}
else if(categoryParam=="dog"){
if(dogVal<=20){
dogVal+=modBase;
}
}
else if(categoryParam=="fox"){
if(foxVal<=20){
foxVal+=modBase;
}
}
else if(categoryParam=="bird"){
if(birdVal<=20){
birdVal+=modBase;
}
}
else if(categoryParam=="memes"){
if(memesVal<=20){
memesVal+=modBase;
}
}
else if(categoryParam=="jokes"){
if(jokesVal<=20){
jokesVal+=modBase;
}
}
else if(categoryParam=="redpanda"){
if(redpandaVal<=20){
redpandaVal+=modBase;
}
}
else if(categoryParam=="koala"){
if(koalaVal<=20){
koalaVal+=modBase;
}
}
else if(categoryParam=="raccoon"){
if(raccoonVal<=20){
raccoonVal+=modBase;
}
}
else if(categoryParam=="kangaroo"){
if(kangarooVal<=20){
kangarooVal+=modBase;
}
}
}
/*
function to call when user selects dislike, decreases the modifier value of the corresponding image
if the user dislikes more than 20 times, the modifier will have larger magnitude than the random
generated number so it becomes virtually impossible for the user to see images from that category
which was left on purpose since if a user dislikes a category that many times it is unlikely they
want to see it anymore
*/
function dislikeImage(categoryParam){
if(categoryParam=="panda"){
pandaVal-=modBase;
}
else if(categoryParam=="cat"){
catVal-=modBase;
}
else if(categoryParam=="dog"){
dogVal-=modBase;
}
else if(categoryParam=="fox"){
foxVal-=modBase;
}
else if(categoryParam=="bird"){
birdVal-=modBase;
}
else if(categoryParam=="memes"){
memesVal-=modBase;
}
else if(categoryParam=="jokes"){
jokesVal-=modBase;
}
else if(categoryParam=="redpanda"){
redpandaVal-=modBase;
}
else if(categoryParam=="koala"){
koalaVal-=modBase;
}
else if(categoryParam=="raccoon"){
raccoonVal-=modBase;
}
else if(categoryParam=="kangaroo"){
kangarooVal-=modBase;
}
}