30秒学会 JavaScript 片段 – isDivisible
Checks if the first numeric argument is divisible by the second one.
Use the modulo operator (%
) to check if the remainder is equal to 0
.
继续阅读 30秒学会 JavaScript 片段 – isDivisible
分享文章、钻研技术、改变生活!
收集只需要30秒便可学会的代码片段!翻译自 https://30secondsofcode.org
Checks if the first numeric argument is divisible by the second one.
Use the modulo operator (%
) to check if the remainder is equal to 0
.
继续阅读 30秒学会 JavaScript 片段 – isDivisible
Converts an angle from degrees to radians.
Use math.Pi
and the degree to radian formula to convert the angle from degrees to radians.
Gets the day of the year from a Date
object.
Use new Date()
and Date.prototype.getFullYear()
to get the first day of the year as a Date
object, subtract it from the provided date
and divide with the milliseconds in each day to get the result.
Use Math.floor()
to appropriately round the resulting day count to an integer.
继续阅读 30秒学会 JavaScript 片段 – dayOfYear
Checks if the given value is equal to negative zero (-0
).
Checks whether a passed value is equal to 0
and if 1
divided by the value equals -Infinity
.
继续阅读 30秒学会 JavaScript 片段 – isNegativeZero
Groups the elements into two arrays, depending on the provided function’s truthiness for each element.
Use Array.prototype.reduce()
to create an array of two arrays.
Use Array.prototype.push()
to add elements for which fn
returns true
to the first array and elements for which fn
returns false
to the second one.
继续阅读 30秒学会 JavaScript 片段 – partition
Converts a tilde path to an absolute path.
Use String.prototype.replace()
with a regular expression and OS.homedir()
to replace the ~
in the start of the path with the home directory.
继续阅读 30秒学会 JavaScript 片段 – untildify
Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.
Use Array.prototype.map()
to reorder arguments based on indexes
in combination with the spread operator (...
) to pass the transformed arguments to fn
.
继续阅读 30秒学会 JavaScript 片段 – rearg
Curries a function.
Use recursion.
If the number of provided arguments (args
) is sufficient, call the passed function fn
.
Otherwise, return a curried function fn
that expects the rest of the arguments.
If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. Math.min()
), you can optionally pass the number of arguments to the second parameter arity
.
继续阅读 30秒学会 JavaScript 片段 – curry
Converts a string to title case.
Use String.replaceAllMapped()
to break the string into words and capitalize the first letter of each word, using a RegExp
.
Use String.replaceAll()
to replace invalid separator characters (-
and _
) with spaces.
继续阅读 30秒学会 Dart 片段 – toTitleCase
Creates a striped list with alternating background colors, which is useful for differentiating siblings that have content spread across a wide row.
:nth-child(odd)
or :nth-child(even)
pseudo-class to apply a different background color to elements that match based on their position in a group of siblings.div
, tr
, p
, ol
, etc.继续阅读 30秒学会 CSS 片段 – Zebra striped list