Home > Blockchain >  How to solve it without using 'arguments' but with ...rest?
How to solve it without using 'arguments' but with ...rest?

Time:03-30

My teacher said that i can solve it without using arguments in const args, but with using ...rest im not really good with this operator so i need help with that. Use rest and give types to all arguments

function destroyer(arr: number[]) {
  const args = arr.slice.call(arguments,1);

  function remove(toDel: number) {
    return args.indexOf(toDel) === -1;
  }
  return arr.filter(remove);
}

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

(This function remove argumets after array from that array, and output [2, 2] here)

CodePudding user response:

You can use Rest syntax

function destroyer(arr, ...args) {
  function remove(toDel) {
    return args.indexOf(toDel) === -1
  }
  return arr.filter(remove)
}

const result = destroyer([1, 2, 3, 1, 2, 3], 1, 3)

console.log(result)

Using simpler approach

function destroyer(a, ...b) {
  return a.filter(x => !b.includes(x))
}

const result = destroyer([1, 2, 3, 1, 2, 3], 1, 3)

console.log(result)

  • Related