-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (26 loc) · 783 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
// Will load the compliments as an array
const compliments = require('./compliments.json');
// A simple function that will get a specific compliment in the array
// The first one will have the index 0
function get(index) {
// Make sure the index is valid, that is inside the array
if (index < 0 || index > compliments.length - 1) {
throw new RangeError(
'The index provided was out of range. Please use a number between 0 and ' + (compliments.length - 1)
);
}
return compliments[index];
}
function getRandomNumber(min, max) {
return min + Math.floor(Math.random() * max);
}
function random() {
return get(
getRandomNumber(0, compliments.length-1)
);
}
// Export the functions for others to use
module.exports = {
get: get,
random: random,
};