• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

154
Vistas
How to short the expression?

I have that expression

if (a === Infinity && b === 0 || a === -Infinity && b === 0 || a === 0 && b === Infinity || a === 0 && b === -Infinity) {
        return NaN
    }

I want short it, but I have no idea how to do this

UPDATE

If it possible, I cant use isFinite(), how to shorten else?

about 3 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use !isFinite() to test if it's either Infinity or -Infinity.

if ((!isFinite(a) && b === 0) || (!isFinite(b) && a === 0)) {
    return NaN;
}
about 3 years ago · Juan Pablo Isaza Denunciar

0

If a and b are number typed, then:

if (!(a*b || isFinite(a+b))) return NaN;

If your linter warns about use of global functions, then:

if (!(a*b || Number.isFinite(a+b))) return NaN;

If you can't use multiplication:

if (!(a && b || Number.isFinite(a+b))) return NaN;
about 3 years ago · Juan Pablo Isaza Denunciar

0

Math.absing both and then checking that the minimum is 0 and the maximum is infinity should do it.

const nums = [a, b].map(Math.abs);
if (Math.min(...nums) === 0 && Math.max(...nums) === Infinity) {
  return NaN;
}

Can also sort the array.

const nums = [a, b]
  .map(Math.abs)
  .sort(a, b) => a - b); // .sort() works too, but could be more confusing
if (nums[0] === 0 && nums[1] === Infinity)) {
  return NaN;
}
about 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda