scrimba
Frontend Career Path
Working with APIs
Async JS
Callbacks - make own filterArray function
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 - make own filterArray function
AboutCommentsNotes
Callbacks - make own filterArray function
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)


/**
* Challenge:
*
* Write your own `filter` function! Don't worry about adding it to the prototype of arrays or anything.
* This function should take 2 parameters:
* 1. The array you want to filter through, and
* 2. A callback function
*
* Steps for filterArray function logic:
* 1. Initialize a new, empty array which will be returned at the end of the `filterArray`s operations (Completed ✅)
* 2. Loop through the array passed as the 1st parameter
* 3. Inside the loop, call the callback function, passing the individual item you're currently looping over as the argument to your callback function
* 4. If the callback function returns `true`, push the current item you're iterating on in the loop to the new array. If it returns `false`, don't push it to the array.
* 5. When the loop is over, return the new array
*/

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
}

// We'll do this later
// const peopleWithPets = filterArray(people, /*???*/)
Console
/index.html
-6:06