Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Kyu 8 Code Wars - Finding the Sum of an array after removing the highest and lowest values

I am practising on code wars and am currently stuck on a kyu 8 question, all the tests seem to pass bar the last one. I will add my code and the tests below plus the output I get below.

function sumArray(array) {

  if (array == null || array.length <= 2) {
    return 0
  } else {

    let largestInt = Math.max.apply(null, array)
    let smallestInt = Math.min.apply(null, array)
    let indexSmallest = array.indexOf(largestInt)
    let indexLargest = array.indexOf(smallestInt)

    array.splice(indexSmallest, 1)
    array.splice(indexLargest, 1)

    let sum = 0

    for (let i = 0; i < array.length; i++) {
      sum += array[I]
    }

    return sum

  }
}


The tests:

const {
  assert
} = require("chai");

it("example tests", () => {
  assert.strictEqual(sumArray(null), 0);
  assert.strictEqual(sumArray([]), 0);
  assert.strictEqual(sumArray([3]), 0);
  assert.strictEqual(sumArray([3, 5]), 0);
  assert.strictEqual(sumArray([6, 2, 1, 8, 10]), 16);
  assert.strictEqual(sumArray([0, 1, 6, 10, 10]), 17);
  assert.strictEqual(sumArray([-6, -20, -1, -10, -12]), -28);
  assert.strictEqual(sumArray([-6, 20, -1, 10, -13]), 3);
});

The output:

Test Results: example tests expected -10 to equal 3

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

function total(array) {
  // always assure at least an empty array.
  array = Array.from(array ?? []);

  // sort array values ascending.
  array.sort((a, b) => a - b);

  array.pop();    // remove last/higest value.
  array.shift();  // remove first/lowest value.

  // for any to be reduced/summed-up (empty) array
  // the initial value of zero always assures the
  // minimum expected result of zero.
  return array
    .reduce((total, value) => total + value, 0);
}

const testEntries = [
  [ null, 0 ],
  [ [ ], 0 ],
  [ [ 3 ], 0 ],
  [ [ 3, 5 ], 0 ],
  [ [ 6, 2, 1, 8, 10 ], 16 ],
  [ [ 0, 1, 6, 10, 10 ], 17 ],
  [ [ -6, -20, -1, -10, -12 ], -28 ],
  [ [ -6, 20, -1, 10, -13 ], 3 ],
];
console.log(
  testEntries
    .map(([value, result]) =>
      `(total([${ value }]) === ${ result }) ... ${ total(value) === result }`
    )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda