#3: Refactoring a callback function

#3: Notes on Refactoring a callback function

By Larissa Cury in R

April 30, 2023

Shall we start with notes on Refactoring a callback function ?

Aim: Rewrite the same callbackfn separately and pass it as an argument to others callback functions

Ps: Notes based on Jonas Schmedtmann’s course on Udemy – No credits intended

#1: .filter(), .some() and .every()

// Create arrays: 

const countriesSpelled = ['B','r','a','z','i','l']
const countries = ['Brazil', 'Denmark', 'USA', 'Italy', 'Colombia', 'Argentina']
const countriesPOP = [214.3, 5.857, 331.9, 59.11, 51.52, 45.81] // in milions of people

// Create a function to return only populations above 200 milion:

const isBiggerThan200 = pop => pop > 200;

// In Use:

// Filter:
console.log(countriesPOP.filter(isBiggerThan200)); // [214.3, 331.9]
// Some:
console.log(countriesPOP.some(isBiggerThan200)); // T
// Every:
console.log(countriesPOP.every(isBiggerThan200)); // F

🐕 Au-au! This is a good way to stick to DYI!