0

I've wrote a graph measurement library using TypeScript called EdgeJS. I've also wrote unit test using Jasmine to verify this it is correct.

The problem is that I have failing test and root cause, I think, is that I have a function to initialize arrays filled with zero instead of undefined and that initialization method seem to "remember" the array it has initialized which affect the results.

You can see in the console output of the unit test page that this line print out arrays with numbers and not just zero as I expected.

1 Answer 1

1

I have put together this short example.

  1. I don't have any problem getting arrays of different lengths back
  2. I don't have any problem with it remembering any values if I change them

If you can supply details of the console output you are getting from your EdgeJS code, that might help to pin this down.

Example

class ArrayUtilities {
    private arrayOfZeros(n: number) : number[] {
        var a: number[] = [];
        for (var i = 0; i < n; i++) { 
            a[i] = 0;
        }
        console.log("New array", a);
        return a;
    }

    test() {
        var a: number[] = this.arrayOfZeros(5);
        for (var i = 0; i < a.length; i++) {
            a[i] = 1;
        }
        console.log("Updated array", a);
        var b: number[] = this.arrayOfZeros(6);
        var c: number[] = this.arrayOfZeros(2);
    }
}

var au = new ArrayUtilities();
au.test();

I get the following output...

New array[0, 0, 0, 0, 0]app.js?1 (line 8)
Updated array[1, 1, 1, 1, 1]app.js?1 (line 16) 
New array[0, 0, 0, 0, 0, 0]app.js?1 (line 8)
New array[0, 0]app.js?1 (line 8)
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, I put the immediately executing function to try and overcome this problem. How is that possible that one line after I create and zero-out the array it has values, what am I missing?
Let me create a smaller test project to see if I can recreate the issue and isolate the cause.
Thank you very much, my code is open in github with link in the question.
Quick question... what do you see in your console, i.e. "new array"... something?
new-arr [0.25, 0, 0.25, 0.5] Edge.js:120 new-arr [2, 2, 2, 2] Edge.js:120 new-arr [1.3333333333333333, 1.3333333333333333, 1.3333333333333333, 1.3333333333333333] Edge.js:120 new-arr [2.5, 0.5, 0, 0.5] Edge.js:120 new-arr [0.5, 2.5, 0.5, 0] Edge.js:120 new-arr [0, 0.5, 2.5, 0.5] Edge.js:120 new-arr [0.5, 0, 0.5, 2.5]
|

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.