-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
62 lines (48 loc) · 1.8 KB
/
Copy pathmain.js
File metadata and controls
62 lines (48 loc) · 1.8 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
52
53
54
55
56
57
58
59
60
61
62
import { futuramaQuotes } from './quotes.js';
let quote = document.querySelector(".quote");
let quoteText = document.querySelector(".quote__text");
let quoteAuthor = document.querySelector(".quote__author");
let btn = document.querySelector(".btn");
let notAccessed = [];
const randomItemFromArray = (myArray) => {
if (notAccessed.length === 0) {
for (let i = 0; i < myArray.length; i++) notAccessed.push(i);
} else if (notAccessed.length >= myArray.length) {
notAccessed = [];
for (let i = 0; i < myArray.length; i++) notAccessed.push(i);
}
let randomIndex = Math.floor(Math.random() * notAccessed.length);
let indexOfItem = notAccessed[randomIndex];
notAccessed.splice(randomIndex, 1);
return myArray[indexOfItem];
};
const getBenderQuote = () => {
// extract Bender quotes from futuramaQuotes array
const benderQuotes = [];
for (let i = 0; i < futuramaQuotes.length; i++) {
if (futuramaQuotes[i].who === 'Bender') {
benderQuotes.push(futuramaQuotes[i].quote);
}
};
// access DOM to change quote text
quoteText.innerText = randomItemFromArray(benderQuotes);
quoteAuthor.innerHTML = "Bender B. Rodríguez";
// change button to active using btn__active class
setTimeout(() => {
btn.classList.add("btn__active");
}, 1000);
}
const newBenderQuote = event => {
if (!event.target.matches(".btn")) return;
getBenderQuote();
btn.classList.remove("btn__active");
}
window.addEventListener("load", getBenderQuote);
quote.addEventListener("click", newBenderQuote);
// const getBenderQuote = async () => {
// const response = await fetch("https://bender.sierrasoftworks.com/api/v1/quote/bender");
// const data = await response.json();
// quoteText.innerText = data.quote;
// quoteAuthor.innerText = "Bender";
// btn.classList.add("btn__active");
// }