Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
Sum without highest and lowest number

I've been spending a few hours trying to understand where the error is in this code. Debugging indicated that the output is 0 as it should be for any number less than 0. However, this code is still unable to pass the test:

function sumArray(array) 
{
  return array < 1 || array == null ? 0
    :array.reduce((a, b) => a + b) - Math.max(...array) - Math.min(...array);
  
  }

console.log(sumArray([-3]))
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

function sumValues (array) {
if (array != null && array.length > 0) {    
  let min = Math.min(...array);  // lowest in array by spreading
  let max = Math.max(...array);  // highest in array by spreading

  // Sum up the numbers in the array
  let sum = array.reduce((acc, val) => acc + val, 0);

  // Do the math
  let result = sum - max - min;
  if (result > 0) {
      return result
  } else {
      return 0
  }
 
} else {
    return 0
}
}

console.log(sumValues([1,2,3,4,5,6]))
about 4 years ago · Santiago Gelvez Relatório

0

Definitely better:

function sumArray(array) {
  
  if (array == null || array.length <= 1) {
    return 0
  }
  
  var max = Math.max(...array);
  var min = Math.min(...array);
  var sum = 0
  
  for (i = 0; i < array.length; i++) {
    sum += array[i];
   }

  return sum - max - min
}
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda