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

261
Visualizações
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 Respostas
Responde à pergunta

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 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