-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (79 loc) · 2.89 KB
/
index.js
File metadata and controls
99 lines (79 loc) · 2.89 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { catsData } from '/data.js'
const emotionRadios = document.getElementById('emotion-radios')
const getImageBtn = document.getElementById('get-image-btn')
const gifOption = document.getElementById('gifs-only-option')
const memeModalInner = document.getElementById('meme-modal-inner')
const memeModal = document.getElementById('meme-modal-inner')
getImageBtn.addEventListener('click', renderCat)
// so we want to add an event listener to emotionRadios to help us highlight our option
emotionRadios.addEventListener('change', highlightCheckedOption)
// we add a fxn that adds the styling (highlight) when we click on the radio and also listen for change
function highlightCheckedOption(e){
// so we want to to remove the styling when we click on another option
const radios = document.getElementsByClassName("radio")
document.getElementById(e.target.id).parentElement.classList.remove('highlight')
for (let radio of radios){
radio.classList.remove('highlight')
}
document.getElementById(e.target.id).parentElement.classList.add('highlight')
}
// we want to return an array of cat objects that matches the users criteria
function getMatchingCatsArray(){
if(document.querySelector('input[type = "radio"]:checked')){
const selectedEmotion = document.querySelector('input[type="radio"]:checked').value
const isGif = gifOption.checked
const matchingCatsArray = catsData.filter(function(cat){
if (catsArray.length === 1){
return catsArray[0]
}
else {
const randomNumber = Math.floor(Math.random() * catsArray.length)
return catsArray[randomNumber]
}
})
console.log(matchingCatsArray)
}
}
// we want to return a single cat object selected from the array provided by getMatchingCatsArray
function getSingleCatObject(){
}
// we want t use the cat object provided by getSingleCatObject to create HTML string which will render it to the DOM
function renderCat(){
const catObject = getSingleCatObject()
memeModal.innerHTML =
`<img
class="cat-img"
alt="${catObject.alt}"
alt="CAT ALT TEXT"
>
`
}
function getEmotionsArray(cats){
const emotionsArray = []
for (let cat of cats){
for (let emotion of cat.emotionTags){
if(!emotionsArray.includes(emotion)){
emotionsArray.push(emotion)
}
}
}
return emotionsArray
}
function renderEmotionsRadios(cats){
let radioItems = ``
const emotions = getEmotionsArray(cats)
for (let emotion of emotions){
radioItems += `
<div class="radio">
<label for="${emotion}">${emotion}</label>
<input
type="radio"
id="${emotion}"
value="${emotion}"
name="emotions"
>
</div>`
}
emotionRadios.innerHTML = radioItems
}
renderEmotionsRadios(catsData)