function lastOrUndefined(array) {
        if (array.length === 0) {
            return undefined;
        }
        return array[array.length - 1];
    }
Returns the last element of an array if non-empty, undefined otherwise.