Skip to content

Instantly share code, notes, and snippets.

@gvinter
Created July 7, 2011 14:13
Show Gist options
  • Save gvinter/1069598 to your computer and use it in GitHub Desktop.
Save gvinter/1069598 to your computer and use it in GitHub Desktop.
underscore exercise: use head and tail
_= require("/Users/gvinter/WebDev/cloud-application-engineer-1/lectures/week05/javascript/underscore.js")
// Exercise: use head and tail in javascript to implement a recursive function over a list of elements
//console.log(_.first([1,2,3]));
function last(an_array){
var an_array = new Array();
if (!an_array) {
return nil;
}
if (!an_array[1]) {
return _.first(an_array);
}
else {
last(_.rest(an_array));
}
}
console.log(last([1,2,3]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment