I want to output an array of random values, and array of factorials.
abstract class AClass {
Numbers: Array<number>;
NumbersFact: Array<number>;
constructor(Numbers: Array<number>, NumbersFact: Array<number>) {
this.Numbers = Numbers;
this.NumbersFact = NumbersFact;
for (var i = 0; i < 10; ++i) Numbers[i] = i;
Numbers = this.fill(Numbers);
NumbersFact = this.factorial(Numbers);
console.log(Numbers);
console.log(NumbersFact);
}
Everything works. console.log showed two arrays
fill(array: number[]): number[] {
// Add random numbers to [Numbers]
var tmp: number, current: number, top = array.length;
if (top) while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
First array with random values from 0 to 9
factorial(array: Array<number>): Array<number> {
// Create factorial's array from [Numbers].
var arrayFact = [];
for (var i = 0; i < array.length; ++i) {
const fact = (x: number) => {
return (x == 0) ? 1 : x * fact(x - 1);
}
arrayFact.push(fact(array[i]));
}
return arrayFact
}
Second array with with factorial values from first array
abstract sort(): void;
}
class Class1 extends AClass {
sort(): void { }
}
var test = new Class1([], []);
console.log(test);
console.log(test.Numbers);
console.log(test.NumbersFact);
Result
console.log in the constructor:
- (10) [0, 8, 4, 6, 3, 7, 1, 9, 2, 5],
- (10) [1, 40320, 24, 720, 6, 5040, 1, 362880, 2, 120].
consol.log at the end of the code:
- Class1 {Numbers: Array(10), NumbersFact: Array(0)}
- (10) [0, 8, 4, 6, 3, 7, 1, 9, 2, 5]
- []
Why is the second array empty?
Because
assigns the result of the function call to the
NumbersFactmethod parameter, not tothis.NumbersFact.The reason
seemed to do the right thing is because unlike the
factorialmethod,fillmodifies the empty array you passed in as an arg. You had earlier assigned that array tothis.Numbers:The return value of
fillalso got assigned to a method parameter, not to thethis.Numbers.In other words, the first array was not empty for the wrong reasons.
Lessons to take away:
Your array methods should either modify their inputs and return void, or they should return new arrays and leave their inputs untouched.
The only exception to this rule is if you are creating chainable methods/functions.
Understand that array variables are references, like object variables. They don't hold the actual array.