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
trying to figure out which code is better to write

beginner javascript programmer here, I was wondering which code is considered better to write:

the two ways I wrote are inside the code block, if there is a better way to write this code please lmk.

const mark = {
  fullName: "Mark Miller",
  mass: 78,
  height: 1.69,

  calcBMI: function () {
    return (this.bmi = this.mass / this.height ** 2);
  },
};

const john = {
  fullName: "John Smith",
  mass: 92,
  height: 1.95,

  calcBMI: function () {
    return (this.bmi = this.mass / this.height ** 2);
  },
};

// 1st WAY -------------------------------
const higherBMI =
  mark.calcBMI() > john.calcBMI()
    ? `${mark.fullName}'s BMI (${mark.calcBMI()}) is higher than ${
        john.fullName
      }'s (${john.calcBMI()})!`
    : `${john.fullName}'s BMI (${john.calcBMI()}) is higher than ${
        mark.fullName
      }'s (${mark.calcBMI()})!`;

console.log(higherBMI);

// 2nd WAY -----------------------
mark.calcBMI(); 
john.calcBMI();

if (mark.bmi > john.bmi) {
  console.log(
    `${mark.fullName}'s BMI (${mark.calcBMI()}) is higher than ${
      john.fullName
    }'s (${john.calcBMI()})!`
  );
} else {
  console.log(
    `${john.fullName}'s BMI (${john.calcBMI()}) is higher than ${
      mark.fullName
    }'s (${mark.calcBMI()})!`
  );
}

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

0

You should calculate BMI only once and store the result instead of calculating it every time you need it, and calcBMI() shouldn't return anything

mark.calcBMI(); 
john.calcBMI();

if (mark.bmi > john.bmi) {
  console.log(
    `${mark.fullName}'s BMI (${mark.bmi}) is higher than ${
      john.fullName
    }'s (${john.bmi})!`
  );
} else {
  console.log(
    `${john.fullName}'s BMI (${john.bmi}) is higher than ${
      mark.fullName
    }'s (${mark.bmi})!`
  );
}

Other valid solution is

mark.calcBMI(); 
john.calcBMI();

const higher = mark.bmi > john.bmi ? mark : john;
const lower = mark.bmi > john.bmi ? mark : john;
console.log(
  `${higher.fullName}'s BMI (${higher.bmi}) is higher than ${lower.fullName}'s (${lower.bmi})!`
);

PS: your assume that both bmi values are different

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