Home > Software design >  Array with 10 numbers excluding n numbers from different array
Array with 10 numbers excluding n numbers from different array

Time:01-23

I have an array like this:

oldArray = [1, 3, 6, 7];

And I want to create a new array with a length of 9 and that does not contain the digits in the old array like this:

newArray = [2, 4, 5, 8, 9, 10, 11, 12, 13, 14];

All in TypeScript. I've tried some thing with a for loop but did not get there. Any tips?

CodePudding user response:

//create new empty array
newArray = [];

//use a counter variable to count through the numbers
let counter = 1;

while(newArray.length < 9) {

    //Add current counter element if it does not exist in old array
    if(oldArray.indexOf(counter) == -1) {
        newArray.push(counter);
    }
    counter  ; //increment counter
}
  • Related