Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda