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

130
Visualizações
Why is this function call returning undefined as well

I have written a simple JavaScript code to print the max number of an array and get undefined printed as well.

let arrNums = [1,2,5,19,20];
const maxNum = function(arr) {
if (arr.length != 0) {
    console.log(Math.max(...arr));
}
else{
    console.log("Empty");  
}}

console.log(maxNum(arrNums));
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your function doesn't return anything. Return the Math.max() and the 'Empty' for it to work. After that you can just use console.log(maxNum(arrNums)); outside the function. See below for an example of this:

const arrNums = [1, 2, 5, 19, 20];
const maxNum = function (arr) {
    if (arr.length != 0) {
        return Math.max(...arr);
    } else {
        return 'Empty';
    }
}

console.log(maxNum(arrNums));

Hoped this helped!

about 4 years ago · Juan Pablo Isaza 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