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

359
Visualizações
How to return indexes of the filtered values, which equal to num?

function fMI(num,arr){
    let digit = arr.toString().split('');
    let realDigit = digit.map(Number);
    console.log("Is RealDigit an array: ",Array.isArray(realDigit));
    console.log(realDigit.filter((v,i,a) => (v==num)? i:''));
}

fMI(1,[1, 11, 34, 52, 61]);

// Create a function that takes a number and an array of numbers as a parameter // and returns the indices of the numbers of the array which contain the given number // or returns an empty list (if the number is not part of any of the numbers in the array)

Sample output: // console.log(findMatchingIndexes(1, [1, 11, 34, 52, 61])); // should print: [0, 1, 4]

I am struggling to return the indexes of the values from an array. The code I have written so far takes one number and compares it to the array which the function takes in. Now, I am at the phase where I want to to return the indexes where the array values equal to the number taken by the function for comparison.

How can I make this work, cause after the filter method, I get only 3 times the number 1, which is not right.

function fMI(num:number,arr:number[]){
       let digit = arr.toString().split('');
       let realDigit = digit.map(Number);
       console.log("Is RealDigit an array: ",Array.isArray(realDigit));
       console.log(realDigit.filter((v,i,a) => (v==num)? i:''));
}

fMI(1,[1, 11, 34, 52, 61]);
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use a traditional for loop:

function fMI(num, arr) {
  let result = [];
  let numArr = arr.map(item => Number(item));
  for (var i = 0; i < numArr.length; i++) {
    if (numArr[i] == num) {
      result.push(i);
    }
  }
  console.log(result);
}

fMI(1, [1, 11, 34, 52, 61]);

about 4 years ago · Juan Pablo Isaza Relatório

0

New answer

You can use the string method includes like so:

function fMI(num: number, arr: number[]) {
  const indexes: number[] = [];
  arr.forEach((value, index) => {
    if (value.toString().includes(num.toString())) {
      indexes.push(index);
    }
  })
  return indexes;
}

fMI(1, [1, 11, 34, 52, 61]); // [0, 1, 4]

Old answer

You can use forEach with a second argument that is the element index. Note that you are specifying arr as an array of number, so there's no need to convert them to numbers again.

function fMI(num: number, arr: number[]) {
  const indexes: number[] = [];
  arr.forEach((value, index) => {
    if (value === num) {
      indexes.push(index);
    }
  })
  return indexes;
}

The 4 lines you have written doesn't help with the logic, that's why I removed it. If you want to know why, leave a comment on this answer and I will reply your questions.

about 4 years ago · Juan Pablo Isaza Relatório

0

Here's an interesting take:

function fMI(num,arr) {
       return arr
              .map((n) => n.toString().split('')
              .map((m) => parseInt(m)))
              .map((o, i) => o.some((p) => p === num) ? i : null)
              .filter((f) => f !== null);
}

const result = fMI(1,[1, 11, 34, 52, 61]);

console.log(result);

First we go through all the numbers in the array, convert them to strings and split them to replace the original numbers with arrays of digits. Then we run those digit arrays through parseInt it convert them to number arrays. Finally we use .some to check if any of the digits in the digit array is num and return an array of indexes or nulls which we run through a filter that removes the nulls and finally we return an array of the indexes for where any of the digits of the number is the same as num.

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