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

103
Visualizações
Why Js function returning [59, f]

let p2 = function(arr, index,value)
{
    let array1 = [5,3,6,1,8,5,8,32];
    array1 = arr;
    return [
        array1[index] = value,
        function count()
        {
            for(let i=0; i<array1.length; i++)
            {
                console.log(array1[i]);
            }
        }
    ]
};
console.log(p2(arr, 3,59));

In this code I'm trying the print the array after replacing the value but I;m getting [59,f]. Idk what f is here.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have the following return :

return [
        array1[index] = value,
        function count()
        {
            for(let i=0; i<array1.length; i++)
            {
                console.log(array1[i]);
            }
        }
    ]

So the JS engine returns an array, where 59 is from the statement array1[index] = value and the f is the function that is at the 1st index of the returned array, which is :

function count()
        {
            for(let i=0; i<array1.length; i++)
            {
                console.log(array1[i]);
            }
        }

Not sure what your intention which function count is, but you would probably want to move the return statement at last to get some meaningful value.

[Update]:

You cannot return 2 values from a non-generator function, so it's better to return an object that has your desired result, and use it after the function call completes :

let arr = [5,3,6,1,8,5,8,32];

let p2 = function(arr, index,value)
{
    let array1 = [...arr];
    array1[index] = value;
    return {value: array1[index], res: arr} 
};

const { value, res } = p2(arr, 3, 59);
console.log(value, res);

about 4 years ago · Santiago Trujillo 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