Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
I'm trying to exclude elements from an Array, but I'm doing somthing wrong

I'm trying to create this script: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy

After the comment "Verify and delete" is where I have the problem. I try to check all the elements form the array 'theCheck' with the elements from 'destroyers' and if the elements dont match then the script will push that value to the output array.

But it pushes every element regardless.

Expected output value: [1,1]

Current output value value: [1,2,3,1,2,3]

function destroyer(arr) {

  let theCheck = [];
  let destroyers = [];
  let output = [];

  for (let i = 1; i < arguments.length; i++) {
    destroyers.push(arguments[i]);
  }

  for (let i = 0; i < arguments[0].length; i++) {
    theCheck.push(arguments[0][i])
  }

  //Verify and delete
  var j = 0
  for (let i = 0; i < theCheck.length; i++) {
    for (j = 0; j < destroyers.length; j++) {
      if (theCheck[i] !== destroyers[j]) {
        output.push(theCheck[i])
        break;
      }
    }
  }

  console.log(theCheck)
  console.log(destroyers)
  console.log(output)
  return arr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

With the current code you're looping through the destroyers and anytime you find a destroyer that doesn't match the item you're checking you're adding it to the output. But because you've got two items in the destroyers array it is guaranteed that one of the two is not going to match the particular item that you're checking.

Below is a version where we work out whether any of the destroyers are found to match the item that we're checking, and only if it doesn't we're adding it to the output:

function destroyer(arr) {

  let theCheck = [];
  let destroyers = [];
  let output = [];

  for (let i = 1; i < arguments.length; i++) {
    destroyers.push(arguments[i]);
  }

  for (let i = 0; i < arguments[0].length; i++) {
    theCheck.push(arguments[0][i])
  }

  //Verify and delete
  var j = 0
  for (let i = 0; i < theCheck.length; i++) {
    let found = false;
    for (j = 0; j < destroyers.length; j++) {
      if (theCheck[i] === destroyers[j]) {
        found = true;
      }
    }
    if(!found) output.push(theCheck[i]);
  }

  console.log(theCheck)
  console.log(destroyers)
  console.log(output)
  return arr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

You could use the includes function to tidy this up a little:

function destroyer(arr) {

  let theCheck = [];
  let destroyers = [];
  let output = [];

  for (let i = 1; i < arguments.length; i++) {
    destroyers.push(arguments[i]);
  }

  for (let i = 0; i < arguments[0].length; i++) {
    theCheck.push(arguments[0][i])
  }

  //Verify and delete
  var j = 0
  for (let i = 0; i < theCheck.length; i++) {
    if(!destroyers.includes(theCheck[i]))
      output.push(theCheck[i]);
  }

  console.log(theCheck)
  console.log(destroyers)
  console.log(output)
  return arr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

about 4 years ago · Juan Pablo Isaza Denunciar

0

function destroyer(input, ...arr) {
   return input.filter(element => !arr.includes(element)); 
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda