Skip to content

Instantly share code, notes, and snippets.

@electricjesus
Created February 19, 2015 06:16
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 electricjesus/179f33b9b433d39fcb25 to your computer and use it in GitHub Desktop.
Save electricjesus/179f33b9b433d39fcb25 to your computer and use it in GitHub Desktop.
/* Functional Programming 101
1.) Create a function 'last' that has the following use cases:
- last("abc") // --> outputs "c"
- last(1,2,3,"D") // --> outputs "D"
- last([1,2,3,4]) // --> outputs 4
2.) Create a function named 'insertUnique' that accepts any object and ensures all objects in the resulting / current array is unique.
var myarray = [];
var myArray = insertUnique("a"); // results to myArray equals to ["a"]
myArray = insertUnique("a"); // results to myArray still equals to ["a"]
myArray = insertUnique({"a": 2}); // results to myArray now equals to ["a", {a: 2}]
BONUS
POINTS: turn insertUnique into a prototyped function. e.g.:
var myArray = [];
myArray.insertUnique("a"); // results to myArray equals to ["a"]
myArray.insertUnique("a"); // results to myArray still equals to ["a"]
myArray.insertUnique({"a": 2}); // results to myArray now equals to ["a", {a: 2}]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment