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

218
Visualizações
Números divisibles por 3 en JavaScript

Estoy tratando de hacer una calculadora científica más avanzada. Tengo problemas para averiguar cómo hacer este tipo de cálculo. Tengo una matriz de números y quiero verificar cuáles de los números dados son divisibles por 3.

Me gustaría tanto la colección de números divisibles por 3 como el conteo de esos números.

Por ejemplo, en la entrada de:

 const collection = [ { text: "0", value: 0 }, { text: "1", value: 1 }, { text: "2", value: 2 }, { text: "3", value: 3 }, { text: "4", value: 4 }, { text: "5", value: 5 }, { text: "6", value: 6 }, { text: "7", value: 7 }, { text: "8", value: 8 }, { text: "9", value: 9 }, { text: "10", value: 10 }, { text: "11", value: 11 }, { text: "12", value: 12 }, ];

Me gustaría obtener una salida como:

 "There are 5 divisible numbers by 3: 0, 3, 6, 9, 12"

¿Cómo puedo hacer eso?

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

0

Use el operador de módulo con bucles for, indicado por %

about 4 years ago · Juan Pablo Isaza Relatório

0

Tengo una matriz de números y quiero verificar cuáles de los números dados son divisibles por 3.

Tiene una matriz de objetos que contienen un número en la clave de value .


Podemos obtener la salida deseada por

  1. filter() solo divisible por 3
    filter(c => c.value % 3 === 0)
  2. map() para obtener solo el número de la clave de value
  3. join() para separar los resultados con una coma

 const collection = [{ text: "0", value: 0 }, { text: "1", value: 1 }, { text: "2", value: 2 }, { text: "3", value: 3 }, { text: "4", value: 4 }, { text: "5", value: 5 }, { text: "6", value: 6 }, { text: "7", value: 7 }, { text: "8", value: 8 }, { text: "9", value: 9 }, { text: "10", value: 10 }, { text: "11", value: 11 }, { text: "12", value: 12 }, ]; let res = collection.filter(c => c.value % 3 === 0).map(o => o.value); console.log(`There are ${res.length} divisible numbers by 3: ${res.join(', ')}`);

Resultado

 There are 5 divisible numbers by 3: 0, 3, 6, 9, 12

Si queremos excluir 0 , podemos cambiar el filter() a:

 let res = collection.filter(c => c.value > 0 && c.value % 3 === 0).map(o => o.value);
about 4 years ago · Juan Pablo Isaza Relatório

0

Puede usar el método de filter para filtrar los objetos que contienen un número divisible por 3, usando el operador de módulo (%). También he usado el map para crear una matriz de valores, en lugar de objetos.

 const collection = [ { text: "0", value: 0 }, { text: "1", value: 1 }, { text: "2", value: 2 }, { text: "3", value: 3 }, { text: "4", value: 4 }, { text: "5", value: 5 }, { text: "6", value: 6 }, { text: "7", value: 7 }, { text: "8", value: 8 }, { text: "9", value: 9 }, { text: "10", value: 10 }, { text: "11", value: 11 }, { text: "12", value: 12 }, ]; let divby3 = collection.filter((x)=>x.value % 3 === 0).map((x)=>x.value) console.log(`There are ${divby3.length} divisible numbers by 3: ${divby3}`)

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