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

159
Visualizações
function that takes a nested array and element as an argument, return a new array, if a nested array doesn't contain element it will push it to newArr

function filteredArray(arr, elem) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; i++) {
      if (arr[i][j] != elem) {
        newArr.push(arr[i]);
      }
    }
  }
  return newArr;
}

console.log(filteredArray([
  [3, 2, 3],
  [1, 6, 3],
  [3, 13, 26],
  [19, 3, 9]
], 3));

How should I remove that error? If a nested array doesn't contain element it will push it to newArr.

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

0

  1. You need to increment j in that inner loop.

  2. You also need to find a way to check to see if elem has been found, and only if it's been found, to add the nested array to the output.

function filteredArray(arr, elem) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    let found = false;
    for (let j = 0; j < arr[i].length; j++) {
      if (arr[i][j] === elem) {
        found = true;
      }
    }
    if (!found) newArr.push(arr[i]);
  }
  return newArr;
}

console.log(JSON.stringify(filteredArray([
  [3, 2, 3],
  [1, 6, 3],
  [1, 6, 4],
  [3, 13, 26],
  [19, 3, 9]
], 3)));

A more modern method would be to filter out the arrays that where elem is not included:

function filteredArray(arr, elem) {
  return arr.filter(inner => {
    return !inner.includes(elem);
  });
}

console.log(JSON.stringify(filteredArray([
  [3, 2, 3],
  [1, 6, 3],
  [1, 6, 4],
  [3, 13, 26],
  [19, 3, 9]
], 3)));

about 4 years ago · Juan Pablo Isaza Relatório

0

function filteredArray(arr, elem) {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr[i].length; j++) {
            if (arr[i][j] == elem) {
                newArr.push(arr[i]);
            }
        }
    }
    return newArr;
}

console.log(filteredArray([
    [3, 2, 3],
    [1, 6, 2],
    [3, 13, 26],
    [19, 3, 9]
], 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