Intermediate Algorithm Scripting - Seek and Destroy

Tell us what’s happening:
my code works but instead of having [1, 1] as output it has

[ [ 1 ], [ 1 ] ]

Your code so far

function destroyer(arr) {
  const sample = Object.values(arguments);
  // console.log(sample)
  const array = sample.filter(x => Array.isArray(x) === true );
  // console.log(array[0])
  const toBeRemoved = sample.filter(x => Array.isArray(x) === false);
  // console.log(toBeRemoved)
    const targets = [];
    const safe = [];
  for( let i = 0; i < array[0].length; i++){
    if(toBeRemoved.indexOf(array[0][i]) !== -1){
        targets.push(i);
    }
  }
  for(let a = 0; a < array[0].length; a++){
    if(targets.indexOf(a) === -1 ){
      if(a == 0){
        let safetyMarkStart = array[0].slice(0,1);
        safe.push(safetyMarkStart);
      }
      else{
        let anyOtherSafetyMark = array[0].slice(a, a+1);
        safe.push(anyOtherSafetyMark);
      }
    }
  }
  console.log(targets)
  return safe;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Seek and Destroy

Link to the challenge:

slice () returns an array with the element(s) sliced.

Hey ereal3755,

Bones is correct, you are essentially pushing two separate arrays let safetyMarkStart = array[0].slice(0,1); and let anyOtherSafetyMark = array[0].slice(a, a+1); into another const safe = [];

you can fix this by applying the flat method to your safe return statement like so:

 console.log(targets)
  return safe.flat();
}

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

hope that helps!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.