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.
- lastElement([3,5,7]) //7
- lastElement([1]) //1
- lastElement([]) //null
** the code that I wrote **
let array = [3, 5, 7];
function lastElement (array) {
return array[array.length - 1];
}
I'm flummoxed on the last part with the function returning null if the array if empty.