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

197
Visualizações
Dealing with multiple optional arguments in a javascript function

first time posting, basically doing the Odin project Exercise 04 - removeFromArray excercise:

https://github.com/TheOdinProject/javascript-exercises/tree/main/04_removeFromArray

I have got the first test to pass, however, i have no idea on how to write the program to deal with multiple optional arguments. I know i need to use the ...arg, but do not really understand how to start or what to do, or the general construction of the function, please can someone help me to understand how to go about this? Below is a very poor attempt by me which doesnt work (not even sure if my logic is correct to get the desired output):

const removeFromArray = function (array1, ...args) {

    if (args == 3) {
        array1.splice(2, 1);
        return array1;
    } else if (args == 3 || args == 2) {
        array1.splice(2, 1);
        return array1.splice(1,1);
    }
};


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

any help or advice on how to do this would be greatly appreciated.Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

args is an array of arguments. So you just need to filter the original array and filter out those elements that are in array args:

function removeFromArray(array, ...args) {
    return array.filter(x => !args.includes(x))
};


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

gives you:

[1, 4]
about 4 years ago · Juan Pablo Isaza Relatório

0

args is an array, so you can loop it via forEach and remove items from your array1

const removeFromArray = function (array1, ...args) {

  args.forEach((arg) => {
    const index = array1.indexOf(arg);
    if (index > -1)
      array1.splice(index, 1);
  });
  return array1;
    
};


console.log(removeFromArray([1, 2, 3, 4], 3, 2, 5));

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