-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (25 loc) · 836 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
import adjectives from "adjectives";
import getRandomAnimal from "animals";
import pluralize from "pluralize";
const getTwoRandomAdjs = (array) => {
if (array.length < 2) {
throw new Error("Oh no no");
}
const first = array[Math.floor(Math.random() * array.length)];
let second = first;
while (first === second) {
second = array[Math.floor(Math.random() * array.length)];
}
return [first, second];
};
export const generatePhrase = () => {
const plAnimal = pluralize(getRandomAnimal());
const alliterativeAdjectives = adjectives.filter(
(adj) => adj[0] === plAnimal[0]
);
const [adj1, adj2] = getTwoRandomAdjs(alliterativeAdjectives);
const capitalisedPhrase = [adj1, adj2, plAnimal]
.map((word) => word[0].toUpperCase() + word.substring(1))
.join(" ");
return capitalisedPhrase;
};