If you only need to know what the outcome was & don't need the modified value of myvar in further application flow, do any math involved inside the conditional & test the variables value against the anonymous result returned by the equation.
// Declare variable
var myvar:Number = 10;
// Setup either a positive or negative number randomly
var polarity:Number = Math.round(Math.random()) === 1 ? 1 : -1;
// Test the value of 'myvar' against an equation using the random polarity
if(myvar > myvar+polarity){
trace('Went down \nmyvar : '+ myvar + '\npolarity : '+polarity);
}else{
trace('Went up \nmyvar : '+ myvar + '\npolarity : '+polarity);
}
// Note: For AS1, remove ':Number' from the variables
- // Declare variable
- var myvar:Number = 10;
- // Setup either a positive or negative number randomly
- var polarity:Number = Math.round(Math.random()) === 1 ? 1 : -1;
- // Test the value of 'myvar' against an equation using the random polarity
- if(myvar > myvar+polarity){
- trace('Went down \nmyvar : '+ myvar + '\npolarity : '+polarity);
- }else{
- trace('Went up \nmyvar : '+ myvar + '\npolarity : '+polarity);
- }
- // Note: For AS1, remove ':Number' from the variables
If you need the use the modified value of myvar in further application flow, hold reference to the variable before doing any math, then compare the reference against the modified myvar as I believe lostinbeta insinuated.
// Declare the 'spotlight var'
var myvar:Number = 10;
// Declare a variable to hold reference before equations
var myvarref:Number;
// Stack for testing purposes
var mystack:Number = 5;
// Declare the loop iterator beforehand
var i:Number = 0;
for(; i<10; i++){
// Save the reference
myvarref = myvar;
// Modify myvar randomly
myvar += Math.round(Math.random()) === 1 ? 1 : -1;
// Test conditional, do somthing involving the variable
if(myvar > myvarref){
mystack += myvar;
}else{
mystack += myvarref;
}
}
trace(mystack);
// Note: For AS1, remove ':Number' from the variables
- // Declare the 'spotlight var'
- var myvar:Number = 10;
- // Declare a variable to hold reference before equations
- var myvarref:Number;
- // Stack for testing purposes
- var mystack:Number = 5;
- // Declare the loop iterator beforehand
- var i:Number = 0;
- for(; i<10; i++){
- // Save the reference
- myvarref = myvar;
- // Modify myvar randomly
- myvar += Math.round(Math.random()) === 1 ? 1 : -1;
- // Test conditional, do somthing involving the variable
- if(myvar > myvarref){
- mystack += myvar;
- }else{
- mystack += myvarref;
- }
- }
- trace(mystack);
- // Note: For AS1, remove ':Number' from the variables
Strong with this one, the sudo is.