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

421
Vistas
What the logic behind b1 ? b1 : b2 in my coding in if-conditions?

1.Here is the assignment.

Declare a function or that works like ||, but without using the || operator.
/**

  1. @param {any} ??? - the first operand
  2. @param {any} ??? - the second operand
  3. @returns {any} the same result as applying the || operator to the given operands, in order

2.Here is what tried(actually people gave me advice on return b1 ? b1 : b2.But i couldn't understand it ,and haven't found a proper explanation about it online.

function or(b1, b2) {
  if (b1 == false && b2 == false) return false;
  else return b1 ? b1 : b2;
}

3.Here are the coding tests, and the code above passed all of them. But would anyone tell me the logic of b1 ? b1 : b2. I am a beginner, please help me !

//TEST 1

actual = or("bananas", false);

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 2

actual = or("", "bananas");

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 3

actual = or(true, true);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 4

actual = or(true, false);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

b1 ? b1 : b2

This means that if b1 is true/valid it will simply returns the result due to code of b1 otherwise it will simply return the result due to code of b2

about 4 years ago · Juan Pablo Isaza Denunciar

0

b1 ? b1 : b2 is using a Ternary Operation

If you were to write it as a if-else statement it would look like

if (b1 === true) {
  return b1;
} else {
  return b2;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

function or(b1, b2) {
  if (b1 == false && b2 == false) return false;
  else return b1 ? b1 : b2;
}

Multiple things are happing Headers. Here, You have a test function with falsy check. Falsy is validateNumber, it can be null, blank string or zero. The condition will always return false. If you really looking to test value with the type you should use === instead.

console.log(Boolean("")) //false
console.log(Boolean(null))//false

console.log(Boolean(0))//false

console.log(Boolean(" "))//true
console.log(Boolean("1"))//true
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