I want to create a random array with different types, but I won't return the same type twice.
I have 6 class for example that inherited from another super class :
Here are the classes, lets say they are empty and contain nothing just for example:
class PrimaryClass {
}
class A extands PrimaryClass {
}
class B extands PrimaryClass {
}
class C extands PrimaryClass {
}
class D extands PrimaryClass {
}
class E extands PrimaryClass {
}
class F extands PrimaryClass {
}
I have a function randomObject()
that returns a random object : (in the main.ts)
function getRandomobject() : PrimaryClass {
let randomNumber : number = Math.floor(Math.random() * (6)) + 1; // random num between 1-6
switch (randomNumber) {
case 1 : return new A();
case 2 : return new B();
case 3 : return new C();
case 4 : return new D();
case 5 : return new E();
case 6 : return new F();
}
}
Now , in the main.ts class I create an array with length 3 for example :
let arr : PrimaryClass [] = new Array <PrimaryClass>(3);
I want to fill the array with random types like :
- A , B , C
- B , D , E
- F , A , B
But don't repeat the same type twice.
I checked the type of objects I created:
let typeA: A = new A(); // Type of is object
let typeB: B = new B(); // Type of is object
let typeC: C = new C(); // Type of is object
I want to fill the array with simple loop :
for (let i = 0 ; i< arr.length ; i++) {
arr[i] = getRandomobject();
}
So, all types are objects so how can I check that something is no longer created in the array?
I want to compare every object created from the function to existing objects already, but how do I check its type? (Check type A,B,C...)
Aucun commentaire:
Enregistrer un commentaire