5) You have not shown how to use the function, and it will only delay the execution of code chained with the Promise. Statements immediately after will proceed as normal with no delay.
6) Your input is missing the key.
7) Your input shown is wrong, and this can also be shorter - using the built-in method designed for the purpose:
8) Will not function correctly as it will never return #ffffff, and also many other colours as you are not padding the string with leading zeroes. A correct version:
You're correct; my initial example did not show how to actually use the function in a meaningful way. It's a Promise-based function, so you'd typically use it with async/await or .then() to actually cause a delay in the code's execution. Here's how you could use it:
(async()=>{console.log("Before waiting");awaitchill(2000);console.log("After waiting for 2 seconds");})();
2. The sortBy Function
You bring up a good point. The function could indeed be simplified to:
This approach assumes that a[key] and b[key] will never be equal if sorting is based on unique identifiers, which might be okay for some cases but not all.
3. The pluck Function
You're right; the input was incomplete without specifying the key. A more full example could be:
Your point is valid; the input example could have been more complete. Also, your shorter version using splice is more concise. However, one benefit of the original slice approach is that it does not mutate the original array.
You are correct again; the original version can indeed skip some colors and never reach #FFFFFF. Your version with padding zeros is more accurate in generating the full spectrum of colors:
Thank you for pointing out these details; your observations are insightful and enhance the accuracy and efficiency of the examples.
I will update it according to it!
Try the shorter sortBy with repeated identifiers. Works just fine. If the two identifiers are equal it makes no difference what order they are sorted in.
My splice example doesn't mutate the original array.
You're right that if two elements have the same identifier, the order doesn't matter for those particular elements. In many sorting algorithms, including JavaScript's native .sort(), elements that compare as equal remain in their original order (stable sort). So, your simplified version is equally effective:
This will work just fine even with repeated identifiers.
2. The splice Example and Array Mutation
I apologize for the misunderstanding. You're correct; your example using splice actually doesn't mutate the original array because you made a shallow copy (a=[...arr]) before applying the splice.
In this function, a=[...arr] creates a new array that is a copy of the original arr. Then, splice is called on this new array (a), so the original arr remains unchanged. Therefore, it is a non-mutating operation on the original array.
Thank you for your keen observations; they serve to improve the quality and accuracy of the discussion.
3) can be shorter:
5) You have not shown how to use the function, and it will only delay the execution of code chained with the Promise. Statements immediately after will proceed as normal with no delay.
6) Your input is missing the
key.7) Your input shown is wrong, and this can also be shorter - using the built-in method designed for the purpose:
8) Will not function correctly as it will never return
#ffffff, and also many other colours as you are not padding the string with leading zeroes. A correct version:1. The
chillFunctionYou're correct; my initial example did not show how to actually use the function in a meaningful way. It's a
Promise-based function, so you'd typically use it withasync/awaitor.then()to actually cause a delay in the code's execution. Here's how you could use it:2. The
sortByFunctionYou bring up a good point. The function could indeed be simplified to:
This approach assumes that
a[key]andb[key]will never be equal if sorting is based on unique identifiers, which might be okay for some cases but not all.3. The
pluckFunctionYou're right; the input was incomplete without specifying the key. A more full example could be:
4. The
insertFunctionYour point is valid; the input example could have been more complete. Also, your shorter version using
spliceis more concise. However, one benefit of the originalsliceapproach is that it does not mutate the original array.5. The
randomColorFunctionYou are correct again; the original version can indeed skip some colors and never reach
#FFFFFF. Your version with padding zeros is more accurate in generating the full spectrum of colors:Thank you for pointing out these details; your observations are insightful and enhance the accuracy and efficiency of the examples.
I will update it according to it!
Try the shorter
sortBywith repeated identifiers. Works just fine. If the two identifiers are equal it makes no difference what order they are sorted in.My
spliceexample doesn't mutate the original array.Might want to fix the glaring typo in the header image too! 😉
@jonrandy which one?
"8 One-Linear You Will Love"
ohh yes you are correct! 😊
I am open for any other suggestions
1. The
sortByFunction with Repeated IdentifiersYou're right that if two elements have the same identifier, the order doesn't matter for those particular elements. In many sorting algorithms, including JavaScript's native
.sort(), elements that compare as equal remain in their original order (stable sort). So, your simplified version is equally effective:This will work just fine even with repeated identifiers.
2. The
spliceExample and Array MutationI apologize for the misunderstanding. You're correct; your example using
spliceactually doesn't mutate the original array because you made a shallow copy (a=[...arr]) before applying thesplice.Here's the function for clarity:
In this function,
a=[...arr]creates a new array that is a copy of the originalarr. Then,spliceis called on this new array (a), so the originalarrremains unchanged. Therefore, it is a non-mutating operation on the original array.Thank you for your keen observations; they serve to improve the quality and accuracy of the discussion.
#3: when sorting numbers just use subtraction for numbers just do
a - bfor ascending orderand for strings use
a.localeCompare(b)Yes, this is also correct!