scrimba
Frontend Career Path
Working with APIs
Async JS
Callbacks - put our custom filterArray function to use
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

Callbacks - put our custom filterArray function to use
AboutCommentsNotes
Callbacks - put our custom filterArray function to use
Expand for more info
index.js
run
preview
console
function handleClick() {
fetch("https://apis.scrimba.com/deckofcards/api/deck/new/shuffle/")
.then(res => res.json())
.then(data => console.log(data))
}

document.getElementById("new-deck").addEventListener("click", handleClick)

// function callback() {
// console.log("I finally ran!")
// }

// setTimeout(callback, 2000)

// const people = [
// { name: "Jack", hasPet: true },
// { name: "Jill", hasPet: false },
// { name: "Alice", hasPet: true },
// { name: "Bob", hasPet: false },
// ]

// function gimmeThePets(number) {
// return person.hasPet
// }

// const peopleWithPets = people.filter(gimmeThePets)
// console.log(peopleWithPets)

const people = [
{ name: "Jack", hasPet: true },
{ name: "Jill", hasPet: false },
{ name: "Alice", hasPet: true },
{ name: "Bob", hasPet: false },
]

function filterArray(array, callback) {
const resultingArray = []
// Write your filtering logic here
for (let item of array) {
const shouldBeIncluded = callback(item)
if (shouldBeIncluded) {
resultingArray.push(item)
}
}
return resultingArray
}

/**
* Challenge: Use your filter array method!
* Given the above `people` array, return a new array with only people where `hasPet` is true
* Note: Remember that your callback function will be given the individual item in the array for a parameter
*/

const peopleWithPets = filterArray(people, /*???*/)
Console
/index.html
-4:17