Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

43
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?

7 months 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;
}
7 months 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;
7 months 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;
}
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos