Removing specific values from an array
This helper function lets us remove all instances of specific values from an array.
Read full articleWant to use JavaScript to make Spongebob memes? This function transforms "strings like this"
into "StRiNgS lIkE tHiS"
. It turns the first letter into uppercase, and then alternates between lowercase and uppercase for all following letters.
const spongebobify = string => {
return string.split("").reduce(({ result, isUppercase }, char) => {
// check if the character is a letter from a to z (upper- or lowercase)
if (/[a-zA-Z]/.test(char)) {
// change the casing of the letter
const letter = isUppercase ? char.toUpperCase() : char.toLowerCase()
// append updated letter and flip `isUppercase` for next iteration
return {
result: `${result}${letter}`,
isUppercase: !isUppercase
}
}
// append unchanged non-letter (like numbers), keep `isUppercase` as is
return {
result: `${result}${char}`,
isUppercase
}
}, {
// these are the default values we go into the reduce-function with
result: "",
isUppercase: true
}).result
}
// this has very little practical use
spongebobify("string like this") // ⇒ StRiNg LiKe ThIs
spongebobify("This is a seaplane.") // ⇒ ThIs Is A sEaPlAnE.
const spongebobify = string => {
return string.split("").reduce(({ result, isUppercase }, char) => {
// check if the character is a letter from a to z (upper- or lowercase)
if (/[a-zA-Z]/.test(char)) {
// change the casing of the letter
const letter = isUppercase ? char.toUpperCase() : char.toLowerCase()
// append updated letter and flip `isUppercase` for next iteration
return {
result: `${result}${letter}`,
isUppercase: !isUppercase
}
}
// append unchanged non-letter (like numbers), keep `isUppercase` as is
return {
result: `${result}${char}`,
isUppercase
}
}, {
// these are the default values we go into the reduce-function with
result: "",
isUppercase: true
}).result
}
// this has very little practical use
spongebobify("string like this") // ⇒ StRiNg LiKe ThIs
spongebobify("This is a seaplane.") // ⇒ ThIs Is A sEaPlAnE.
const spongebobify = string => {
return string.split("").reduce(({ result, isUppercase }, char) => {
// check if the character is a letter from a to z (upper- or lowercase)
if (/[a-zA-Z]/.test(char)) {
// change the casing of the letter
const letter = isUppercase ? char.toUpperCase() : char.toLowerCase()
// append updated letter and flip `isUppercase` for next iteration
return {
result: `${result}${letter}`,
isUppercase: !isUppercase
}
}
// append unchanged non-letter (like numbers), keep `isUppercase` as is
return {
result: `${result}${char}`,
isUppercase
}
}, {
// these are the default values we go into the reduce-function with
result: "",
isUppercase: true
}).result
}
// this has very little practical use
spongebobify("string like this") // ⇒ StRiNg LiKe ThIs
spongebobify("This is a seaplane.") // ⇒ ThIs Is A sEaPlAnE.
const spongebobify = string => {
return string.split("").reduce(({ result, isUppercase }, char) => {
// check if the character is a letter from a to z (upper- or lowercase)
if (/[a-zA-Z]/.test(char)) {
// change the casing of the letter
const letter = isUppercase ? char.toUpperCase() : char.toLowerCase()
// append updated letter and flip `isUppercase` for next iteration
return {
result: `${result}${letter}`,
isUppercase: !isUppercase
}
}
// append unchanged non-letter (like numbers), keep `isUppercase` as is
return {
result: `${result}${char}`,
isUppercase
}
}, {
// these are the default values we go into the reduce-function with
result: "",
isUppercase: true
}).result
}
// this has very little practical use
spongebobify("string like this") // ⇒ StRiNg LiKe ThIs
spongebobify("This is a seaplane.") // ⇒ ThIs Is A sEaPlAnE.
This helper function lets us remove all instances of specific values from an array.
Read full articleBy destructuring the return value of a function, we can take out only the values we need without having to assign the result to an object first.
Read full articleThe ternary operator can be used at many levels. The further we move it into a statement, the more duplication it can save us.
Read full articleWhen creating Tailwind CSS-like utility classes, we can use Sass’ loops to save a lot of repetition.
Read full articleBy combining regular expressions and parseFloat, we can create a helper function that extracts all float values from a text input.
Read full articleWe can write a single function that reads the global theme instead of doing the same thing for multiple properties.
Read full article