- Return a Sorted Array without changing the Original Array
var globalArray = [5, 6, 3, 2, 9]; function nonMutatingSort(arr) { return [].concat(arr).sort((a,b) => a-b); } nonMutatingSort(globalArray);
- A side effect of the
sort
method is that it changes the order of the elements in the original array. One way to avoid this is to first concatenate an empty array to the one being sorted (remember thatconcat
returns a new array), then run thesort
method.
- Combine an Array into String Using the join Methodreplace를 사용하지 않고 split과 join만 사용하기+) split 메소드에 정규식 바로 쓸 수 있는 거 처음 알았다…
function sentensify(str) { return str.split(/\W/).join(' '); }
sentensify("I-like-Star-Wars"); // should return "I like Star Wars"
- Use the every Method to Check that Every Element in an Array Meets a Criteria
var numbers = [1, 5, 8, 0, 10, 11]; numbers.every(function(currentValue) { return currentValue < 10; }); // Returns false
- Use the some Method to Check that Any Elements in an Array Meet a Criteria
var numbers = [10, 50, 8, 220, 110, 11]; numbers.some(function(currentValue) { return currentValue < 10; }); // Returns true
- Introduction to Currying and Partial ApplicationThis is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available.Similarly,
partial application
can be described as applying a few arguments to a function at a time and returning another function that is applied to more arguments. //Impartial function function impartial(x, y, z) { return x + y + z; } var partialFn = impartial.bind(this, 1, 2); partialFn(10); // Returns 13
// Call a curried function in parts: var funcForY = curried(1); console.log(funcForY(2)); // Prints 3
//Un-curried function function unCurried(x, y) { return x + y; } //Curried function function curried(x) { return function(y) { return x + y; } } curried(1)(2) // Returns 3
Reference: freeCodeCamp Functional Programming