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

213
Visualizações
How to test whether a string represents a "problematic" number?

I have a large array, named data, of string values from an external source, where each is supposed to represent an integer.

When I leave it to Javascript's automatic type conversion, there were no Javascript errors when I ran some arithmetic calculations on the whole array.

However, an operation like if (data[i] > data[i-1]) count++; gives the wrong result for some of the array entries. I don't know which.

When I added the following manual conversion step, then computation on the array of numbers gave the expected result:

data.forEach((n, i) => {
  data[i] = Number(n);
});

In other words, the same computation on data gives different outcomes when the above step is included or not.

As the array is large, it is not practical for me to comb through the data to pick out the offending ones. I want to find out where the problem is. I modified the forEach loop to:

data.forEach((n, i) => {
  if (data[i] != Number(n)) console.log("!!!", i, n);
  data[i] = Number(n);
});

but nothing was printed out.

What is a method to find out which values of the array are problematic?

data is like: [ "123", "234", "456", "789", .... ].


If you want to know how I knew there was a problem, a colleague used Python on the same set of data and produced a different result. That led to an investigation.

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

0

If you're expecting valid integers in the strings, try this method to discover which ones might be problems:

const data = ["123", "234", "456", "789", "", "2,234", "2.02", "1e3", "n0", "invalid", "2", "10"];

for (const [index, str] of data.entries()) {
  if (parseInt(str, 10) !== Number(str)) console.log(index, str);
}

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