If statement in pre-request script

I have a field in my code that could be 1, 2 or 3, i have set a dynamic variable for that field so that it picks at random the number
i want to assert that if it picks number 2, a different field in my code should populate with the word name and if it picks 1 or 3, that that field remains like this " "

how do i do this?

The field you want to update needs to be a variable as well, probably a collection variable and you can default it to " ".

Sounds like this is a straight forward IF statement. You are only really interested if its a 2, otherwise you won’t take any action.

if ( condition ) {
// statements to be executed
// if condition specified is valid or true
}
if ({{dynamicVariable}} == "2") { // I'm assuming this is a string, not an actual number
pm.collectionVariables.set("myOtherVariable", "Name");
console.log(pm.CollectionVariables.get("myOtherVariable"));
}

You can also tag an else onto the end of this (although not sure its needed if you set the default for the initial variable correctly.

else {
pm.collectionVariables.set("myOtherVariable", " ");
console.log(pm.CollectionVariables.get("myOtherVariable"));
}