Created
July 7, 2011 14:13
-
-
Save gvinter/1069598 to your computer and use it in GitHub Desktop.
underscore exercise: use head and tail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_= 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