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

287
Visualizações
How to get an array of booleans as answer for a parameter "under 10"?

I want to evaluate whether the number is under than 10, having as an answer an array of booleans. I tried using forEach, map, but I didn't succeed in any attempt.

function underTen (arr){
  let underTenAnswer = arr.forEach(function(n){
    if (n < 10) {
      true;
    } else {
      false;
    }
  })
}

Input: underTen([1,9,12,19,4,16] Expected outcome: Array [True, True, False, False, True, False]

I'm a beginner, sorry if something is notoriously incorrect.

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

0

There is a really quick way to do this:

let a = [1, 2, 3, 4, 5, 6, 7];
let b = [1, 2, 3, 4, 12, 8];

console.log(a.every((x) => x < 10)) // true
console.log(b.every((x) => x < 10)) // false

We use Array.every() here to check if each array element passes some condition.

If you need to return an array of booleans, then you can use Array.map():

let b = [1, 2, 3, 4, 12, 8];
console.log(b.map((x) => x < 10)) // [true, true, true, true, false, true]

about 4 years ago · Juan Pablo Isaza Relatório

0

Edit 2: I thought I misunderstood the question but it seems this is what the OP wanted.

You can use the array helper every if you want to know if all array values match an expression:

array.every(number => number < 10);

Edit: I misunderstood the question.

To get an array with boolean values according to an expression applied to each value:

const newArray = array.map(number => number < 10);
about 4 years ago · Juan Pablo Isaza Relatório

0

The question suggests the output is to be an array with boolean values corresponding to each value in the test array.

Array.map() is ideally suited to this problem. Working snippet:

const testArray = [1,9,12,19,4,16];

const resultsArray = testArray.map(element => (element < 10))

console.log(resultsArray)

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