I'm taking Colt Steele's Udemy Course titled: "The Web Developer Bootcamp 2020". I have however become stuck on a certain coding exercise. The exercise objective is as follows: Please write a function called lastElement which accepts a single array argument. The function should return the last element of the array(without removing the element). If the array is empty, the function should return null.
I have tried coming up with a solution but cant seem to figure it out. My current best guess is this :
function lastElement (num) {
if (num !== undefined){
return num[num.length -1];
} return null;
}
I'm interested in knowing why the function I have written doesn't work and some pointers on how I should rethink the function, so that it does work.
Best Regards Andreas