lastElement단일 배열 인수를 허용하는 함수를 작성하십시오 . 함수는 배열의 마지막 요소를 반환해야 합니다(요소를 제거하지 않고). 배열이 비어 있으면 함수는 null을 반환해야 합니다.
lastElement ([ 3 , 5 , 7 ]) //7
마지막 요소 ([ 1 ]) //1
lastElement ([]) // null
function lastElement(arr) {
if (arr.length === 0) {
return null;
} else {
return arr[arr.length - 1];
}
}
console.log(lastElement([1, 4, 6]));