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

157
Visualizações
how to find an element with the parameter as selected and output it in javascript?

I am newbie with javascript and meet a problem like this.

For example . there is a variable with a list like this

{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
},
{
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
}

Find an variable with an attack = 150 and return it in the function "findAMonsterByAttack" If there is no matching variable in the list of variable, the "findAMonsterByAttack" function returns null.

Here is my code :

function findAMonsterByAttack(monsters) {
var monsters = [
{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
},
{
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
},
// ...
];
var isfree = monsters.every(function(course, index) {
return course.attack ===150;
});

My idea is using the code every to check course and index, then return if the attach = 150. But I failed. Could you please help me with this case ? Thank you very much for your time.

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

0

If monsters is an array, you can use array.filter to return another array with of only the monsters with a matching criteria.

eg attack === 150.

Note that there could be multiple matches, so this would return an array with all of the monsters that match, (or an empty array if no monsters match).

const monsters = [{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
  },
  {
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
  },
  {
    name: 'z',
    attack: 150,
    speed: 90,
    hitpoint: 150,
  }
]

const a150 = monsters.filter(m => m.attack === 150)

console.log(a150)

If you only care about one monster, you could also use array.find. But this would return the first monster found and undefined if no matches are found.

const monsters = [{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
  },
  {
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
  },
  {
    name: 'z',
    attack: 150,
    speed: 90,
    hitpoint: 150,
  }
]

const a150 = monsters.find(m => m.attack === 150)

console.log(a150)

Note: array.every tests whether all monsters meet the criteria and returns a Boolean value. In your example this will return false because all monsters do not have an attack value of 150.

about 4 years ago · Juan Pablo Isaza Relatório

0

Is this what you want?

function findAMonsterByAttack() {
var monsters = [
{
    name: 'x',
    attack: 50,
    speed: 100,
    hitpoint: 100,
},
{
    name: 'y',
    attack: 150,
    speed: 80,
    hitpoint: 180,
}
// ...
];

for ( m of monsters ) {
  if ( m.attack == 150 )
    return m
}
return "Monster with attack 150 not found"
}

console.log(findAMonsterByAttack())
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