Update variable only when a condition is satisfied

Hi - this is a newbie question - please forgive if it is very basic or stupid.

There is a variable called NBP which gets computed when a condition is satisfied. Subsequently, if the condition fails, I want the originally computed value to be retained till the condition gets satisfied again - in which case the value will get recomputed and updated.

How do i do this?

The code extract is as follows:

var NBP; if ((objob.NQ + objstock.CFQ) == 0) { NBP = round(((objob.SQ * objob.SP) - (objob.BQ * objob.BP) - objstock.CFV),4); }

objstock.NBP = NBP;

assuming the condition NQ + CFQ == 0 is satisfied, the value gets worked out to say ‘100’.
subsequently, when NQ + CFQ != 0, i want NBP to remain as '100’
later if NQ + CFQ == 0, the formula will be used and say the value is arrived as ‘150’, then NBP should get updated as ‘150’.

the values of NQ, CFQ, SQ, SP, BQ, BP, CFV, etc keeps changing frequently – currently when condition is satisfied, NBP is getting computed and displayed correctly - but when the condition fails the value of NBP becomes ‘0’.

Any help will be highly appreciated.

If you want me to post the whole code, please do let me know.

it sounds like what you want is simply:

if ((objob.NQ + objstock.CFQ) == 0) {
   objstock.NBP = round(((objob.SQ * objob.SP) - (objob.BQ * objob.BP) - objstock.CFV),4);
}

if the condition is satisfied, NBP is updated. If it isn’t, it stays whatever it was. I’m assuming objstock.NBP is just a persistent version of var NBP, because you need something that will last outside of this scope - something other than a local var.

Works perfectly. Thanks a ton.