30秒学会 JavaScript 片段 – curry

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秒学会 Dart 片段 – factorial

Calculates the factorial of an integer.

Use recursion.
If n is less than or equal to 1, return 1.
Otherwise, return the product of n and the factorial of n-1.
Throws an exception if n is a negative number.

30秒学会 JavaScript 片段 – dig

Returns the target value in a nested JSON object, based on the given key.

Use the in operator to check if target exists in obj.
If found, return the value of obj[target], otherwise use Object.values(obj) and Array.prototype.reduce() to recursively call dig on each nested object until the first matching key/value pair is found.

30秒学会 Golang 片段 – LCM

Returns the least common multiple of two or more numbers.

Define a gcd() function that determines the greatest common divisor, using recursion.
Use gcd() and the fact that LCM(x, y) = x * y / GCD(x,y) to determine the least common multiple.
Use range to apply the calculation to all given numbers.

30秒学会 JavaScript 片段 – factorial

Calculates the factorial of a number.

Use recursion.
If n is less than or equal to 1, return 1.
Otherwise, return the product of n and the factorial of n - 1.
Throws an exception if n is a negative number.

30秒学会 JavaScript 片段 – stringPermutations

Generates all permutations of a string (contains duplicates).

⚠️ WARNING: This function’s execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.

Use recursion.
For each letter in the given string, create all the partial permutations for the rest of its letters.
Use Array.prototype.map() to combine the letter with each partial permutation, then Array.prototype.reduce() to combine all permutations in one array.
Base cases are for string length equal to 2 or 1.

30秒学会 JavaScript 片段 – deepFreeze

Deep freezes an object.

Use Object.keys() to get all the properties of the passed object, Array.prototype.forEach() to iterate over them.
Call Object.freeze(obj) recursively on all properties, checking if each one is frozen using Object.isFrozen() and applying deepFreeze() as necessary.
Finally, use Object.freeze() to freeze the given object.

30秒学会 Golang 片段 – GCD

Calculates the greatest common divisor between two or more numbers.

Define a gcd() function for two numbers, which uses recursion.
Base case is when y equals 0, which returns x.
Otherwise the GCD of y and the remainder of the division x/y is returned.
Use gcd() and range to apply the calculation to all given numbers.