Skip to content

Instantly share code, notes, and snippets.

@casdidier
Created December 28, 2021 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save casdidier/f44272f1db82c231dd5f9d4f174e185b to your computer and use it in GitHub Desktop.
Save casdidier/f44272f1db82c231dd5f9d4f174e185b to your computer and use it in GitHub Desktop.
smart-js-ts-snippets
import assert from "assert";
var easyUnpack = (values: any[]): any[] => [
values[0],
values[2],
values[values.length - 2],
];
console.log("Example:");
console.log(easyUnpack([1, 2, 3, 4, 5, 6, 7, 9]));
assert.deepEqual(easyUnpack([1, 2, 3, 4, 5, 6, 7, 9]), [1, 3, 7]);
assert.deepEqual(easyUnpack([1, 1, 1, 1]), [1, 1, 1]);
assert.deepEqual(easyUnpack([6, 3, 7]), [6, 7, 3]);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
/**
* This is my real solution, but if I submit it as an arrow function it is not working
* So, I added the function as a regular one just so it calls my realSolution
**/
const realSolution = ({
0: a,
2: b,
length,
[length - 2]: c,
}: any[]): any[] => [a, b, c];
/** I think most people are going to do something like this: */
const commonEasyUnpack = (e) => [e[0], e[2], e[e.length - 2]];
/** Here is a way for not having to do e.length - 2 */
const IdontWantToDoMathEasyUnpack = (e, [f, , t] = e) => [f, t, e.reverse()[1]];
function easyUnpack(values: any[]): any[] {
return realSolution(values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment