forked from NikhilNamal17/popular-movie-quotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (39 loc) · 1021 Bytes
/
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
let array = require("./data/data.json");
const randomNum = () => {
return Math.floor(Math.random() * array.length);
};
const getQuoteByYear = (start, end) => {
let quotes = [];
array.forEach(item => {
if (item.year && item.year > start && item.year < end) {
quotes.push(item);
}
});
return quotes.sort((a, b) =>
a.year > b.year ? 1 : b.year > a.year ? -1 : 0
);
};
const getRandomQuote = () => {
let randNum = randomNum();
return array[randNum].quote;
};
const getSomeRandom = count => {
let randomQuotesArray = [];
let randomQuotesSet = new Set(); // to prevent duplicate quotes
while (randomQuotesArray.length < count) {
let quote = array[randomNum()];
if (!randomQuotesSet.has(quote)) {
randomQuotesArray.push(quote);
}
}
return randomQuotesArray;
};
const getAll = () => {
return array;
};
module.exports = {
getAll,
getRandomQuote,
getSomeRandom,
getQuoteByYear
};