Can you tell me what im doing wrong?

function buscaDestruye(arreglo, num) {
  // La funcion 'buscaDestruye' recibe como argumento un array de enteros 'arreglo' y un entero 'num'.
  // Esta funcion tiene que eliminar los numeros del array que coincidan con el numero recibido como argumento (num).
  // La función debe retornar el array sin los números sacados.
  // Ej: buscaDestruye([1, 2, 3, 4], 2) devuelve => [1, 3, 4]
  // Nota: Si el numero se repite mas de una vez, tambien hay que eliminarlo.
  // Ej: buscaDestruye([1, 2, 3, 4, 1], 1) devuelve => [2, 3, 4]
  //
  // Tu código aca:

  for (let i = 0; i < arreglo.length; i++) {
    for (let j = 0; j < num.length; j++) {
      if (arreglo[i] === num[j]) {
      return arreglo[i] - num[j];
    }
  }
  }
  };

I think you want to return an array, right? The array should have all of the values in arreglo that are not equal to num. Here, you aren’t returning an array. Also, I don’t think num is an array, it’s a number, right? So you can’t do num[j].

Try using filter() like in your previous challenge

num seems to be a number, not an array, so you can’t do that. A number doesn’t have the length property

You are returning the array once and using a minus operation, as you are passing a number num.length is null so will never entry on the second for, but you don’t need that second for, just iterate over each element of the array in the first for and if the position is equal to num use splice to delete that position.

Note that are more efficients methods to do this like map or filter.

thats what i dont understand really. Couse im recieving an argument with array and a number. So i have to eliminate duplicated number. But i cant figure out hot to do ir. Try a lot of things but nothing works.

I would take @Montin’s advice above and use the filter method on the array to filter out the elements you don’t want. Unless the instructions require you to modify the original array passed in, then you don’t want to use filter because that creates a new array. You didn’t provide a link to the challenge so I don’t know which way is best.

1 Like

I know how to eliminate duplicates in one array. But i cant figure out how to eliminate a number outside an array and compare from one array. The exersice say that i have to compare an array of numbers with another number, but dont know how to compare both of them.

im thinking in join the number and the array into one new array so i would have one array so i can eliminate duplicate from one array.

Can you filter the nums array here so that it doesn’t have the values that are present in removeThese?

let nums = [1, 2, 3, 4, 5]
let removeThese = [2, 4]

i try to do this but didnt work either

  let arregloNuevo = arreglo + num;
  const arregloNuevoArr = new Set(arregloNuevo);
  let sinDuplicado = [...arregloNuevoArr];
  return sinDuplicado;
};

arrays can’t be summed to numbers. If you want to add a number to the end of arreglo you need to use a method to do that

That logic wouldn’t work anyway. Try solving the example I gave you above.

Something like this?

let nums = [1, 2, 3, 4, 5];

let removeThese = [2, 4];

let eliminarDuplicados = nums.filter((removeThese, index)=>{
  return nums.indexOf(removeThese) === index;
})
console.log (eliminarDuplicados);

work with the example you gave me but in the exercise just get error

It’s still returning [ 1, 2, 3, 4, 5 ]

Im just stuck. Can find any answer of how it supose to do.

I would not call the first argument removeThese. The filter method goes through the array one element at a time, and that’s the first argument that is being passed in here. So what would be a better name?

Also, do you really care what the index of that element is? The numbers to be removed from the array can be at any index. I think all you really care about is whether they are in the array, not their position in the array

1 Like