0

I have a function like this:

flag: boolean = false;

some_function(){
                var foo = some_num_value;
                var bar = foo;           // Trying to save value in a separate variable
                if(this.flag){
                               var total = bar + foo;
                             }
                   return total;
                   !this.flag;
               }

This above function runs 2 or 3 times in a single instance. The value of var foo changes for every iteration. I wish to save the value of each var foo such that I need to add it with the new value of var foo when the next iteration happens.

How can I go about this?

1 Answer 1

1

You can move foo to the outside of the method (function).

flag: boolean = false;
foo: number;

some_function(bar){
                
                

                if(this.flag){
                               var total = bar + foo;
                             }
                   this.foo += bar;
                   return total;
                   !this.flag;
               }
Sign up to request clarification or add additional context in comments.

1 Comment

I found one method. I created an array and pushed all iterated values to that array. This array now stores all the values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.