Counting all array elements that match a condition
Passing functions to other functions allows us to condense logic into a few lines of code.
Read full articleIf a piece of code looks cryptic and is difficult to understand, we can put it in a separate function. The descriptive name we’re giving to that function helps explain what it does.
A good name can make a long comment unnecessary.
// We can get today’s date in a specific timezone with `.toLocaleString()`.
// The code looks complicated, and we might not remember what it does two
// months from now.
const now = new Date()
const nowAsString = now.toLocaleString("en-US", {
timeZone: "America/Los_Angeles"
})
nowAsString.match(/\d{1,2}\/\d{1,2}\/\d{4}/)[0]
// ⇒ "8/15/2020"
// Putting this code into a function makes it easily reusable. We don’t need
// to write a comment for it because the name already explains what it does.
// It’s okay to condense the code and skip a few variables in this case.
const getDateInLosAngeles = () => (new Date()).toLocaleString("en-US", {
timeZone: "America/Los_Angeles"
}).match(/\d{1,2}\/\d{1,2}\/\d{4}/)[0]
// Getting the date with one function call is more convenient and readable.
getDateInLosAngeles()
// ⇒ "8/15/2020"
// We can get today’s date in a specific timezone with `.toLocaleString()`.
// The code looks complicated, and we might not remember what it does two
// months from now.
const now = new Date()
const nowAsString = now.toLocaleString("en-US", {
timeZone: "America/Los_Angeles"
})
nowAsString.match(/\d{1,2}\/\d{1,2}\/\d{4}/)[0]
// ⇒ "8/15/2020"
// Putting this code into a function makes it easily reusable. We don’t need
// to write a comment for it because the name already explains what it does.
// It’s okay to condense the code and skip a few variables in this case.
const getDateInLosAngeles = () => (new Date()).toLocaleString("en-US", {
timeZone: "America/Los_Angeles"
}).match(/\d{1,2}\/\d{1,2}\/\d{4}/)[0]
// Getting the date with one function call is more convenient and readable.
getDateInLosAngeles()
// ⇒ "8/15/2020"
// We can get today’s date in a specific timezone with `.toLocaleString()`.
// The code looks complicated, and we might not remember what it does two
// months from now.
const now = new Date()
const nowAsString = now.toLocaleString("en-US", {
timeZone: "America/Los_Angeles"
})
nowAsString.match(/\d{1,2}\/\d{1,2}\/\d{4}/)[0]
// ⇒ "8/15/2020"
// Putting this code into a function makes it easily reusable. We don’t need
// to write a comment for it because the name already explains what it does.
// It’s okay to condense the code and skip a few variables in this case.
const getDateInLosAngeles = () => (new Date()).toLocaleString("en-US", {
timeZone: "America/Los_Angeles"
}).match(/\d{1,2}\/\d{1,2}\/\d{4}/)[0]
// Getting the date with one function call is more convenient and readable.
getDateInLosAngeles()
// ⇒ "8/15/2020"
// We can get today’s date in a specific timezone with `.toLocaleString()`.
// The code looks complicated, and we might not remember what it does two
// months from now.
const now = new Date()
const nowAsString = now.toLocaleString("en-US", {
timeZone: "America/Los_Angeles"
})
nowAsString.match(/\d{1,2}\/\d{1,2}\/\d{4}/)[0]
// ⇒ "8/15/2020"
// Putting this code into a function makes it easily reusable. We don’t need
// to write a comment for it because the name already explains what it does.
// It’s okay to condense the code and skip a few variables in this case.
const getDateInLosAngeles = () => (new Date()).toLocaleString("en-US", {
timeZone: "America/Los_Angeles"
}).match(/\d{1,2}\/\d{1,2}\/\d{4}/)[0]
// Getting the date with one function call is more convenient and readable.
getDateInLosAngeles()
// ⇒ "8/15/2020"
Passing functions to other functions allows us to condense logic into a few lines of code.
Read full articleWhen naming variables that hold Boolean values, using a prefix that indicates as much helps with readability.
Read full articleTo find the largest value in an array of numbers, we can spread that array into Math.max() instead of manually iterating over it.
Read full articleThere is now a more convenient way than reduce or loops to check if all elements in an array match a condition.
Read full articleDestructuring lets us extract an object’s property into a variable of the same name in very little code.
Read full articleWhen your data can contain “outliers” that are clearly unintentional, you can clamp them to all fall within the same valid range.
Read full article