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

83
Vistas
Recode by not using conditional

The below function works good by if statements , I want the same functionality by using without condition statements.

Hello(x){
    If(x<0){
       Console.log("greater");
    }Else{
       Console.log("smaller");
    }
};

Hello(1);
Hello(-1);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could use an Object but it's odd. An if...else statement or it's shorter ternary form more clearly conveys the logic at play which is always preferable.

function Hello(x) {
  const lt = {
    true: "smaller",
    false: "greater"
  };

  console.log(lt[x < 0]);
};

Hello(1);
Hello(-1);

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is probably a stupid thing to actually do and might be overly specific to your example but...:

const hello = (x) => {
  const lt = x < 0;
  console.log("greater".repeat(lt) + "smaller".repeat(1 - lt));
};

hello(-1);
hello(1);

Borrowing from @Teemu's comment a slightly more flexible thing could be:

const if_else = (cond_fn, if_fn, else_fn = (x) => x) =>
  [else_fn, if_fn][+cond_fn()]();

const hello = (x) =>
  if_else(
    () => x < 0,
    () => console.log('greater'),
    () => console.log('smaller')
  );

hello(-1);
hello(1);

about 4 years ago · Juan Pablo Isaza Denunciar

0

@Teemu explained how to do it without branching, and there are times where that can be beneficial (example: Why is processing a sorted array faster than processing an unsorted array?).

function hello(x) {
  console.log(['greater', 'smaller'][+(x < 0)]);
}

hello(1);
hello(-1);

but by definition you cannot achieve behavior based on a condition without a conditional.

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