Doing a Udemy course and need help with this question. Been on it for about a day, I am still really new, and trying to get a good grasp on JavaScript.
Here is the question.
Please write a function called 'lastElement' which appears in 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'.
This is what I have written down so far and it works on my opera browser with Visual Studio Code. Also works with Chrome too, but I want to find out how my instructor wants me to get the right answer. Anyways here is my answer.
function lastElement(arr) {
const jacob = arr[arr.length - 1];
if (arr[arr.length - 1] >= 0) {
console.log(jacob);
} else if (arr.length <= 0) {
console.log(null);
}
}
// These are the values i am checking.
1 | lastElement([3,5,7]); //7
2 | lastElement([1]); //1
3 | lastElement([]); //null"