The function resolves all of the transformations of step correctly, but I can’t seem to write it back to the global variable step. Am I missing something here?
In JavaScript, primitives (like numbers) are passed by value, while objects are passed by reference. In order to do what you’re wanting, you’d have to write something like this:
We’ll need a more complete code sample to figure out what you’re trying to do (i.e. your step declaration, but ideally you’re whole script minus anything sensitive).
From the latest code that you’ve posted step doesn’t look to be global.
If it is global - defined on the same levels as export default function() ... as in :
import http from "k6/http";
let step = 0; // this is global variable
export default function() {
// here you can do w/e with step and if you haven't define one in
// the scope of this function it will change the global one
step++; // this touches the global
}
function touchLocalStep(step) { // here step is an argument and is *not* the global one
step++ // touches the local argument, which is copied by value as argument so it won't be seen outside the function
}
so you can either have it be global or you can have it locally (or have it both, but it will be a different variable).
As far as I can see from your sample you can just step++ wherever you want and when you will be using it you can have a formatStep as in
function formatStep(arg) { // I specifically changed it to not be called `step` here
return arg.toString().padStart(3, '0');
}
and now you can move the definition of step in the default function, so it is local for that function, do step++ where you need to increase it and whenever you need to have it as a string with leading 0 call fortmatStep(step) and use the result instead.