Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
tratando de averiguar qué código es mejor escribir

programador principiante de javascript aquí, me preguntaba qué código se considera mejor escribir:

las dos formas que escribí están dentro del bloque de código, si hay una mejor manera de escribir este código, por favor 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 Respuestas
Responde la pregunta

0

Debe calcular el BMI solo una vez y almacenar el resultado en lugar de calcularlo cada vez que lo necesite, y calcBMI() no debería devolver nada

 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})!` ); }

Otra solución válida es

 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})!` );

PD: asumes que ambos valores de bmi son diferentes

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda