-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (44 loc) · 1.41 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { getMemes, ENDPOINT } from './api.js';
import {
format,
filterMemesLowerThan,
sortResponseInAscendingOrder,
updateMemeTitle
} from './utils.js';
const container = document.querySelector('.container');
const img = document.querySelector('.image-container img');
const memes = getMemes(ENDPOINT)
.then(response => response.map(format))
.then(response => filterMemesLowerThan(response, 500))
.then(sortResponseInAscendingOrder)
.catch(error => console.log(error.message));
memes
.then(response => {
console.dir(response);
return getMemeOfTheDay(response);
})
.catch(error => console.error(error.message));
function getRandomMeme(memes) {
const randomNumber = Math.floor(Math.random() * memes.length);
const memeURL = memes[randomNumber].url;
const memeALT = memes[randomNumber].name;
img.src = memeURL;
img.alt = memeALT;
return memes[randomNumber];
}
function getMemeOfTheDay(arr) {
const currentDate = new Date();
const meme = arr[currentDate.getDate() - 1];
img.setAttribute('src', meme.url);
document.querySelector('.overlay p').innerHTML = meme.name;
}
container.addEventListener('click', element => {
if (element.target.nodeName === 'BUTTON') {
element.target.parentElement.querySelector('h1').innerText = 'Random meme';
element.target.innerText = 'Get another random meme!';
memes
.then(getRandomMeme)
.then(updateMemeTitle)
.catch(error => console.error(error.message));
}
});