scrimba
Frontend Career Path
Essential JavaScript concepts
Meme Picker
Close the modal
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
Close the modal
Expand for more info
index.js
run
preview
console
import { catsData } from '/data.js'

const emotionRadios = document.getElementById('emotion-radios')
const getImageBtn = document.getElementById('get-image-btn')
const gifsOnlyOption = document.getElementById('gifs-only-option')
const memeModalInner = document.getElementById('meme-modal-inner')
const memeModal = document.getElementById('meme-modal')

emotionRadios.addEventListener('change', highlightCheckedOption)

getImageBtn.addEventListener('click', renderCat)

/*
Challenge:
1. Wire up the X button in the modal so
it closes the modal.
*/

function highlightCheckedOption(e){
const radios = document.getElementsByClassName('radio')
for (let radio of radios){
radio.classList.remove('highlight')
}
document.getElementById(e.target.id).parentElement.classList.add('highlight')
}

function getMatchingCatsArray(){
if(document.querySelector('input[type="radio"]:checked')){
const selectedEmotion = document.querySelector('input[type="radio"]:checked').value
const isGif = gifsOnlyOption.checked

const matchingCatsArray = catsData.filter(function(cat){

if(isGif){
return cat.emotionTags.includes(selectedEmotion) && cat.isGif
}
else{
return cat.emotionTags.includes(selectedEmotion)
}
})
return matchingCatsArray
}
}

function getSingleCatObject(){
const catsArray = getMatchingCatsArray()

if(catsArray.length === 1){
return catsArray[0]
}
else{
const randomNumber = Math.floor(Math.random() * catsArray.length)
return catsArray[randomNumber]
}
}

function renderCat(){
const catObject = getSingleCatObject()
memeModalInner.innerHTML = `
<img
class="cat-img"
src="./images/${catObject.image}"
alt="${catObject.alt}"
>
`
memeModal.style.display = 'flex'
}

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)




Console
/index.html
-2:02