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

210
Vistas
Are ternary operators in ternary operatory possible

Why does this line doesnt work

        x > highNum ? highNum = x : y > highNum ? highNum = y : highNum = highNum

In this case this line is in a loop and x and y is different everytime. I tried to find the highest number at the end and thought this would work. In my mind this reads as: If x is higher than high num highnum should get assigned the value of x if not. is y bigger? if yes y should be the new highnum. if not. dont change high num

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

short answer:

Yes, they are.

long answer:

Yes, but you should be concerned about readability too. You code does exactly what you are expecting it to do, but other devs (and maybe you in the future), could have a problem understanding that, so I'd strongly advice you to never use nested ternary operators, and only use them when it makes more sense then a simple if else statement. And if even after all this you still wanna use it, at least add a comment explaining what it does. ex:

  let highNum
  for(let line of lines){
     const {x, y} = line; 
     // use bubble sort to find the highest number
     x > highNum ? highNum = x : y > highNum ? highNum = y : highNum = highNum
  }
  

edit: Also, this is also not correctly finding the highest number, as said by "trincot"

over 4 years ago · Santiago Trujillo Denunciar

0

There is a potential high value that you could miss: when x > highNum, but also y > x, you will not see that y is really the highest, as the expression will already have decided that highNum should get the value of x.

You can do this quite simple with Math.max:

highNum = Math.max(x, y, highNum);
over 4 years ago · Santiago Trujillo Denunciar

0

Yes, but you'll require brackets, mostly for readability:

(x > highNum) ? (highNum = x) : ((y > highNum) ? (highNum = y) : (highNum = highNum));

In your case, it seems you're better off splitting it into multiple statements to prevent confusion:

if (x > highNum) {
    highNum = x;
} else if (y > highNum) {
    highNum = y;
}

although that doesn't fit in a single expression, but perhaps that's a sign of your code getting a bit too complex/unreadable.

If you're solely looking for the highest number, perhaps Math.max is all you need, i.e. Math.max(x, y, highNum).

over 4 years ago · Santiago Trujillo 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