# All files / algorithms/lib sumArray.ts

0% Statements 0/12
0% Branches 0/1
0% Functions 0/1
0% Lines 0/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

     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))