You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
14 lines
438 B
TypeScript
14 lines
438 B
TypeScript
export function sumArray(array: number[] | null): number {
|
|
if (Array.isArray(array) && array.length > 0) {
|
|
array.sort((a, b) => a - b);
|
|
array.shift();
|
|
array.pop();
|
|
return array.reduce((acc, currentValue) => acc + currentValue, 0)
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
console.log(sumArray([6, 2, 1, 8, 10]), 16);
|
|
console.log(sumArray([6, 0, 1, 10, 10]), 17)
|
|
console.log('slice(1, -1): [ 6, 0, 1, 10, 10 ]', [6, 0, 1, 10, 10].slice(1, -1))
|