I am working on an exercise in learning JavaScript, and cannot figure out why my code is failing a test evaluation. And yes, I realize that I am redundantly (and messily) re-defining Underscore.js _.last. That is the purpose of the exercise.
_.last = function(array, n) {
if (n === 0) {
return [];
}
else {
return n === undefined ? array.slice(-1) : array.slice(-n);
};
}'
These are the results I get when running:
_.last([1,2,3], 2)
[2, 3]
_.last([1,2,3], 5)
[1, 2, 3]
_.last([1,2,3], 0)
[]
_.last([1,2,3])
[3]
The very last call _.last([1,2,3]) is the one failing this evaluation:
expect(_.last([1,2,3])).to.equal(3)
Can anyone tell why this expect is returning false? What am I overlooking
Thank you in advance.
EDIT: solved. else edited to return value if n===undefined rather than returning a single-value array:
return n === undefined ? array[array.length-1] : array.slice(-n);
3, but receive an array with the entry 3. i.d.[3]?[ 3 ] !== 3