Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!