Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!