Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
Refactoring A && B && B > A

There is a way to refactor this logic expression?

A && B && B > A

Just learning and I would like to know another way of writing the same expression Thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Split the expressions and put them in variables with readable names.

Due to the lack of information about what A and B are, I assume the first half of the expression makes sure both values are truthy and then you compare them:

const bothTruthy= A && B;
const BLargerThanA = B > A;

const yourExpression = bothTruthy && BLargerThanA;
about 4 years ago · Juan Pablo Isaza Relatório

0

Update:

Okay, the below solution doesn't work for all cases.

I think you don't need to refactor your expression logic if you want A and B to be interpreted as true. During the processing of logical expressions, the engine independently optimizes them and performs a lazy way, for example: if in the expression A && (B || C) the value A is interpreted as false, then the processing of the expression will end immediately and the value A will be returned (false or an equivalent value), but if the value of A is interpreted astrue, then processing of the condition will continue. Further, if B is interpreted as true, then due to logical addition(OR), processing will end with a value equivalent to true, and if B is interpreted as false, then processing will continue...


Try this - A && (B > A)

const cases = [
  [true, false],
  [false, true],
  [false, false],
  [true, true],
  [1, 2],
  [2, 1],
  [1, 0],
  [0, 1],
  [undefined, 1],
  [1, undefined],
  [undefined, 0],
  [0, undefined],
  [null, undefined],
  [undefined, null],
  [1, NaN],
  [NaN, 1],
  [0, NaN],
  [NaN, 0],
  [{}, 0],
  [1, {}],
  [{}, []],
  [[], {}],
  ['2', 1],
  [1, '2'],
  [true, 1],
  [1, true],
  [true, '1'],
  ['1', true],
  [0, -1],
  [-1, 0],
  [1, -1],
  [-1, 1],
  [false, -1],
  [-1, false]
];

const fails = [];

cases.forEach((c) => {
  const [A, B] = c;
  const r1 = A && B && (B > A);
  const r2 = A && (B > A); // <-- simplified condition
  const r = !!r1 === !!r2;
  if (!r) fails.push({c: c, r1, r2});
});

if (fails.length > 0) {
  console.log('Not all cases passed!');
  console.log(fails);
} else {
  console.log('All cases passed!');
}
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda