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

111
Vistas
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 Respuestas
Responde la pregunta

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