0

So im trying to remove the numbers 1,2,7,14 in the array but i dont know how to remove it. I did not find any solution that is similar to this

This is the output of the code

function mySelect(){
 var prime1 = document.getElementById('input1').value;
 var prime2 = document.getElementById('input2').value;
 var n = prime1 * prime2;
 console.log(n);
 var foo = new Array(n);
 console.log(foo.length);
 var range = [];
 
 for(var i=1;i<foo.length;i++){
    range.push(i);
 }
 console.log(range);

 // --------------------------------------------//
 var half = Math.floor(n / 2), // Ensures a whole number <= num.
        str = '1', // 1 will be a part of every solution.
        i, j;

    // Determine our increment value for the loop and starting point.
    n % 2 === 0 ? (i = 2, j = 1) : (i = 3, j = 2);

    for (i; i <= half; i += j) {
        n % i === 0 ? str += ',' + i : false;
    }

    str += ',' + n; // Always include the original number.
    console.log(str);


 
}
Eddie
  • 25,279
  • 6
  • 26
  • 53
wierrr
  • 11
  • 1
  • 1
    You should elaborate where does that number `1, 2, 7, 14` come from? Is it from a pattern or something? Is it a fixed number? – wisn Mar 04 '18 at 16:46

3 Answers3

0

After you pushed the values to the array you can use a filter function, like so:

let nonos = [ 1, 2, 7, 14 ];
range = range.filter((element) => !nonos.includes(element));

This code specifies the values you want removed inside an array and then runs a loop on your original array and checks whether the element you're on is included in your nonos array and if it is, don't include it in your original array, else do.

Alex
  • 35
  • 5
0

find the index first and then apply splice method.

var array=[1,2,3,4,5,6,7,8,9,10]
console.log(array)

find the index of value you want to delete

let indexa=array.indexOf(2);

apply splice method to delete 1 value from applied index

array.splice(indexa,1);
console.log(array)
Aay Que
  • 801
  • 6
  • 14
0

To remove all instances of the provided numbers from an array:

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function removeNumbers(array, ...numbers) {
  numbers.forEach((number) => {
    var index = array.indexOf(number);
    while(index >= 0) {
       array.splice(index, 1); 
       index = array.indexOf(number);
    }
  });

}

removeNumbers(array, 3, 2, 5, 7);
console.log(array);
Giannis Mp
  • 1,266
  • 4
  • 13