scrimba
Frontend Career Path
Essential JavaScript concepts
Meme Picker
isGif
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

index.js
run
preview
console
import { catsData } from '/data.js'

const emotionRadios = document.getElementById('emotion-radios')
const getImageBtn = document.getElementById('get-image-btn')

emotionRadios.addEventListener('change', highlightCheckedOption)

getImageBtn.addEventListener('click', getMatchingCatsArray)

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(){

/*
Challenge:
1. Take control of the gifs only option checkbox.
2. Set up a const in getMatchingCatsArray to store
a boolean which will be set to true if the
"gifs only" option is checked and false if it's
not. (Think what a good name for this const would
be.)
3. Log it out to check it's working.
*/

if(document.querySelector('input[type="radio"]:checked')){
const selectedEmotion = document.querySelector('input[type="radio"]:checked').value
console.log(selectedEmotion)
}
}

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
-3:45