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

250
Views
How to check whether various number ranges are represented in a one-dimensional array of objects?

I am attempting to check whether all age groups from teen to centenarian are represented in an array.

My logic path has been:

  • Create an array with pre-defined decade-based age brackets [1, 2, 3, ... , 10]
  • Isolate the age value of each element in the sample array of objects
  • Divide those ages by 10 to get what decade the age falls in
  • parseInt that number to get an integer and put resultant figures in new array
  • Compare that array with the pre-defined age bracket array created initially.

The expected output is true but I am instead getting false.

Another logic issue I am facing is that the oldest age in the sample array, 128, reduces to 12 after dividing/parsing whereas it would be preferable for it to check if the int is > 10 instead of specifically 10.

Code below:

const devAges = list.map((list) => {
  return list.age
})
const devAgesDiv = devAges.map(i => i / 10);

for(i = 0; i < devAgesDiv.length; i++){
  devAgesDiv[i] = parseInt(devAgesDiv[i]);
}

function allAges(list) {
  const ageBrackets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  return ageBrackets.every((ageBracket) =>
    list.some((listItem) => listItem.ageBracket === ageBracket)
  );
}

console.log(allAges(list)); // output false;

Sample array below:

var list = [
  { firstName: 'Harry', lastName: 'K.', country: 'Brazil', continent: 'Americas', age: 19, language: 'Python' },
  { firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 29, language: 'JavaScript' },
  { firstName: 'Jing', lastName: 'X.', country: 'China', continent: 'Asia', age: 39, language: 'Ruby' },
  { firstName: 'Noa', lastName: 'A.', country: 'Israel', continent: 'Asia', age: 40, language: 'Ruby' },
  { firstName: 'Andrei', lastName: 'E.', country: 'Romania', continent: 'Europe', age: 59, language: 'C' },
  { firstName: 'Maria', lastName: 'S.', country: 'Peru', continent: 'Americas', age: 60, language: 'C' },
  { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 75, language: 'Python' },
  { firstName: 'Chloe', lastName: 'K.', country: 'Guernsey', continent: 'Europe', age: 88, language: 'Ruby' },
  { firstName: 'Viktoria', lastName: 'W.', country: 'Bulgaria', continent: 'Europe', age: 98, language: 'PHP' },
  { firstName: 'Piotr', lastName: 'B.', country: 'Poland', continent: 'Europe', age: 128, language: 'JavaScript' }
];
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can make two updates to get the desired result:

  1. devAgesDiv - check if the list age is over 100 and return 10 otherwise divide by 10; you can parseInt the result of that test

  2. You can use includes in allAges to check every ageBracket is represented

See below:

const list = [
  { firstName: 'Harry', lastName: 'K.', country: 'Brazil', continent: 'Americas', age: 19, language: 'Python' },
  { firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 29, language: 'JavaScript' },
  { firstName: 'Jing', lastName: 'X.', country: 'China', continent: 'Asia', age: 39, language: 'Ruby' },
  { firstName: 'Noa', lastName: 'A.', country: 'Israel', continent: 'Asia', age: 40, language: 'Ruby' },
  { firstName: 'Andrei', lastName: 'E.', country: 'Romania', continent: 'Europe', age: 59, language: 'C' },
  { firstName: 'Maria', lastName: 'S.', country: 'Peru', continent: 'Americas', age: 60, language: 'C' },
  { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 75, language: 'Python' },
  { firstName: 'Chloe', lastName: 'K.', country: 'Guernsey', continent: 'Europe', age: 88, language: 'Ruby' },
  { firstName: 'Viktoria', lastName: 'W.', country: 'Bulgaria', continent: 'Europe', age: 98, language: 'PHP' },
  { firstName: 'Piotr', lastName: 'B.', country: 'Poland', continent: 'Europe', age: 128, language: 'JavaScript' }
];

const devAgesDiv = list.map(item => parseInt(item.age > 100 ? 10 : item.age / 10));

const ageBrackets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function allAges(list) {
  return ageBrackets.every(ageBracket => list.includes(ageBracket));
}

console.log(allAges(devAgesDiv)); // true

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!