-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (63 loc) · 1.84 KB
/
index.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
var jokes = require('./data/jokes.json');
var quotes = require('./data/quotes.json');
var jokesCount = jokes.length;
var quotesCount = quotes.length;
exclude_tags_default = ['sex', 'racist'];
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
const getRandomQuote = () => {
var index = randomInt(0, quotesCount - 1);
return quotes[index];
};
const getRandomJoke = (options = { 'exclude_tags': exclude_tags_default }) => {
const exclude_tags = options['exclude_tags'];
while(true){
const idx = randomInt(0, jokesCount - 1);
let joke = jokes[idx];
let flagged = 0;
for(let i = 0; i < exclude_tags.length; i++){
if(joke.tags.indexOf(exclude_tags[i]) > 0){
flagged = 1;
}
}
if(flagged === 0){
return joke;
}
}
}
const getAllJokesWithTag = (tag) => {
var jokesWithTag = [];
jokes.forEach((joke) => {
if(joke.tags.indexOf(tag) != -1){
jokesWithTag.push(joke);
}
});
return jokesWithTag;
}
const getRandomJokeWithTag =(tag, options = { 'exclude_tags': exclude_tags_default }) => {
var jokesWithTag = getAllJokesWithTag(tag);
const exclude_tags = options['exclude_tags'];
if(jokesWithTag.length == 0){
return {'body' : '', 'tags' : []};
}
while(true){
const idx = randomInt(0, getAllJokesWithTag.length - 1);
let joke = jokesWithTag[idx];
let flagged = 0;
for(let i = 0; i < exclude_tags.length; i++){
if(joke.tags.indexOf(exclude_tags[i]) > 0){
flagged = 1;
}
}
if(flagged === 0){
return joke;
}
}
}
module.exports = {
getRandomJoke,
getRandomQuote,
getAllJokesWithTag,
getRandomJokeWithTag
};