-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
51 lines (35 loc) · 1.22 KB
/
script.js
File metadata and controls
51 lines (35 loc) · 1.22 KB
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
47
48
49
50
51
const emoji = document.getElementById("emoji-icon");
const emojiLabel = document.getElementById("emoji-label");
const EMOJI_API = "https://emoji-api.com/emojis?access_key=";
const EMOJI_CATEGORIES_API = "https://emoji-api.com/categories?access_key=";
const API_KEY = "7e66ebb8f262a8be1a9425ba0574aadfc4e872e8"; //use your own api
let emojis = [];
const random = (max) => Math.floor(Math.random() * max);
const fetchEmoji = async (api) => {
try {
const data = await fetch(api + API_KEY);
emojis = await data.json();
console.log(emojis);
return emojis;
} catch (error) {
console.error("Error :", error);
}
};
const displayRandomEmoji = () => {
if (emojis.length === 0) return;
const randomIndex = random(emojis.length);
console.log(emojis.length);
console.log(randomIndex);
emoji.innerText = emojis[randomIndex].character;
const emojiLabelData = emojis[randomIndex].unicodeName
.split(" ")
.slice(1)
.join(" ");
emojiLabel.innerText =
emojiLabelData.charAt(0).toUpperCase() + emojiLabelData.slice(1);
// console.log(emojis[randomIndex].unicodeName.split(" ").slice(1).join(" "));
};
emoji.addEventListener("click", () => {
displayRandomEmoji();
});
fetchEmoji(EMOJI_API);