1

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"
  • It looks like the task provides you to return a value from the function, not just log it to the console. – Teemu Mar 23 '21 at 13:29
  • I don't wanna just post a solution, rather ask some questions that might help. Check your function, is it `return`ing anything? How would you check if the array is empty (then compare it with what the if/else does in your solution)? – Balázs Édes Mar 23 '21 at 13:30
  • 2
    Please note that your condition is wrong `if (arr[arr.length - 1] >= 0)`. This checks if the last array element is `>= 0`, not if the array is non-empty. – connexo Mar 23 '21 at 13:34
  • There is no reason to close this question because OP did an attempt by themselves and presented it. – connexo Mar 23 '21 at 13:43

2 Answers2

3

You could check the length first and if zero return null, because this is shorter than to get an element and then check the length.

if (arr.length === 0) return null;

For all other cases, return the last element. Here you need no else statement, because of the first condition's return statement.

return arr[arr.length - 1];

function lastElement(array) {
    if (array.length === 0) return null;
    return array[array.length - 1];
}

console.log(lastElement([3, 5, 7])); //    7
console.log(lastElement([1]));       //    1
console.log(lastElement([]));        // null
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

Your instructor wants your function to actually return the last element, not only to console.log it:

function lastElement(arr) {
  return Array.isArray(arr) && arr.length ? arr[arr.length - 1] : null;
}

console.log(lastElement([3, 5, 7])); // 7
console.log(lastElement([1])); // 1
console.log(lastElement([])); // null

The reason that returning a value is useful is that you can e.g. assign the return value to a variable, or pass it to another function.

The latter is what actually happens in my example code:

console.log(lastElement([1]));

executes like this:

lastElement([1]) // this is called first and returns 1

Then the return value 1 is passed to console.log:

console.log(1)

Regarding the syntax I've used, that is called a ternary expression, and it is functionally identical to

if (Array.isArray(arr) && arr.length) { return arr[arr.length - 1]; }
else { return null; }
// same as
return Array.isArray(arr) && arr.length ? arr[arr.length - 1] : null;
connexo
  • 53,704
  • 14
  • 91
  • 128