Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!