-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.js
40 lines (35 loc) · 958 Bytes
/
handlers.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
const quotes = require('./quotes/quotes.js');
function randomQuote() {
return quotes[Math.floor(Math.random() * quotes.length)];
}
function randomQuotes(n) {
var data = [];
var quotesCopy = quotes.slice();
amount = Number(n);
if (amount > quotes.length) {
return {"error": "Specified number exceeds number of quotes. Total number of quotes right now are " + String(quotes.length)};
}
else if (amount < 1 || !Number.isInteger(amount)) {
return {"error": "Invalid number"};
}
else {
while (data.length < amount) {
let index = Math.floor(Math.random() * quotesCopy.length);
data.push(quotesCopy[index]);
quotesCopy.splice(index, 1);
}
return data;
}
}
function allQuotes() {
return quotes;
}
function numOfQuotes() {
return quotes.length;
}
module.exports = {
randomQuote,
allQuotes,
randomQuotes,
numOfQuotes
};