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.
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);