-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.js
33 lines (27 loc) · 977 Bytes
/
handler.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
const jokes = require('./source/jokes.json');
const randomJoke = () => {
return jokes[Math.floor(Math.random() * jokes.length)];
}
/**
* Get N number of random jokes from a jokeArray
*/
const randomN = (jokeArray, n) => {
const limit = jokeArray.length < n ? jokeArray.length : n;
const randomIndicesSet = new Set();
while (randomIndicesSet.size < limit) {
const randomIndex = Math.floor(Math.random() * jokeArray.length);
if (!randomIndicesSet.has(randomIndex)) {
randomIndicesSet.add(randomIndex);
}
}
return Array.from(randomIndicesSet).map(randomIndex => {
return jokeArray[randomIndex];
});
};
const randomTen = () => randomN(jokes, 10);
// Get num number of jokes
const randomNum = (num) => randomN(jokes, num)
const jokeByType = (type, n) => {
return randomN(jokes.filter(joke => joke.type === type), n);
};
module.exports = { jokes, randomNum ,randomJoke, randomN, randomTen, jokeByType };