
Transitive: If compareFn(a, b) and compareFn(b, c) are both positive, zero, or negative, then compareFn(a, c) has the same positivity as the previous two.Ī comparator conforming to the constraints above will always be able to return all of 1, 0, and -1, or consistently return 0.Anti-symmetric: compareFn(a, b) and compareFn(b, a) must both be 0 or have opposite signs.Stable: The comparator returns the same result with the same pair of input.(This is important because there's no guarantee when and how the comparator will be called, so any particular call should not produce visible effects to the outside.) Pure: The comparator does not mutate the objects being compared or any external state.More formally, the comparator is expected to have the following properties, in order to ensure proper sort behavior: Object.prototype._lookupSetter_() Deprecated.Object.prototype._lookupGetter_() Deprecated.Object.prototype._defineSetter_() Deprecated.Object.prototype._defineGetter_() Deprecated.You may refer to the MDN documentation for more info about Spread Syntax. We can solve this using the Spread Syntax as follows. I can use this Spread Syntax to solve this: var arr1 = Īnother main use of Spread Syntax is while copying an array: var arr = I want this to be in a single array like. Let's have an example for this: var arr1 =

Spread Syntax:ĭefinition: Allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected. Splice will return removed elements from the array only.Yes, we have consoled the arr1.splice(1,2,’Hi’,’Medium’). You may think that we have added ‘Hi’,’Medium’ to the array but it doesn't show here…. var arr1 = ģrd Argument ->nth Argument: The value of the items you want to add to the array. If it is not passed, it will delete all elements from the starting position. If it is 0, then no elements will be removed.

If the value is negative the position will be counted from the end of the array.Ģnd Argument: The numbers of elements to be removed from the starting position. Splice:ġst Argument: Specifies at which position a new element or existing element should be added/removed. It will return the original array as it is at the start. See the below Example: var arr1 = Įven if you made some changes to the array it won’t affect it.

Note: Slice always returns the selected elements from the array. If you put a negative number while calling, the selection will be selected from the end of the array. If you didn’t put this in the parenthesis while calling the slice method, it will return the elements from the starting index to the end of the array. Slice:ġst Argument: Specifies from where the selection should be started.įrom the first index (5) it will return the elements.Ģnd Argument: Specifies at which level the endpoint should be. Here, I’m gonna share how I solved it with those methods.īoth Slice and Splice are used to manipulate arrays. I was confused at that time when to use Slice and when to use Splice.
.png)
They already mentioned solving using Slice & Splice. I came across this freeCodeCamp challenge and got stuck for some time thinking about how I could find a way to solve it.
