In the following example the j++ both acts as a variable and a function
var j = 0;
var arr = [];
arr[j++] = "a1";
arr[j++] = "a2";
console.log(arr[0]);
console.log(arr[1]);
is there a way to write this without using the ++ like:
function addx(i)
{
return i+1;
}
var j=0;
arr[addx(j)] = "a1";
arr[addx(j)] = "a2";
the issue is that i can only change the space between the brackets []. I'm not allowed to declare the j variable above the function and update it within so this solution is not acceptable.
var j=0;
function addx(i)
{
j = i+1;
return j-1;
}
arr[addx(j)] = "a1";
arr[addx(j)] = "a2";
In Pascal you can declare the function like function addx(var i:integer) : integer;
in which case the parameter would be the variable so having i := i+1;
inside the function would update the calling variable too.