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

217
Vistas
How can I solve this problem using javascript? do i need to use split?

I am learning JS. suddenly I got the below problem from the internet and I become interested to solve this. But I am getting confused how can I do this? Do I need to use split?

The problem is Using JS, convert:

[ "AND", ["<", "var1", "var2"], [ "OR", [">", "var3", "var4"], ["==", "var5", "var6"] ] } 
To: 
"var1 < val2 AND (var3 > val4 OR val5 == val6)"

Can anyone give me some idea? Thanks

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I would first construct an object whose keys are the operator strings, and whose values are callbacks which, when called, produce the appropriate comparison result.

To avoid eval and global variables, don't use separate variable names, but put them all into an object, so that you can use bracket notation to dynamically look up the appropriate property in the object.

If either operator is an array, use recursion to evaluate those inner values first. Otherwise, you can use the operator string to look up the function to call and call it.

const constants = {
  var1: 1,
  var2: 2,
  var3: 3,
  var4: 4,
  var5: 5,
  var6: 6,
};
const operations = {
  AND: (x, y) => x && y,
  OR: (x, y) => x || y,
  '<': (x, y) => x < y,
  '>': (x, y) => x > y,
  '==': (x, y) => x == y,
  
};

const input = [ "AND", ["<", "var1", "var2"], [ "OR", [">", "var3", "var4"], ["==", "var5", "var6"] ] ];
const evaluate = ([operator, val1, val2]) => {
  val1 = (Array.isArray(val1)) ? evaluate(val1) : constants[val1];
  val2 = (Array.isArray(val2)) ? evaluate(val2) : constants[val2];
  return operations[operator](val1, val2);
};
console.log(evaluate(input));

const constants = {
  var1: 1,
  var2: 2,
  var3: 10,
  var4: 4,
  var5: 5,
  var6: 6,
};
const operations = {
  AND: (x, y) => x && y,
  OR: (x, y) => x || y,
  '<': (x, y) => x < y,
  '>': (x, y) => x > y,
  '==': (x, y) => x == y,
  
};

const input = [ "AND", ["<", "var1", "var2"], [ "OR", [">", "var3", "var4"], ["==", "var5", "var6"] ] ];
const evaluate = ([operator, val1, val2]) => {
  val1 = (Array.isArray(val1)) ? evaluate(val1) : constants[val1];
  val2 = (Array.isArray(val2)) ? evaluate(val2) : constants[val2];
  return operations[operator](val1, val2);
};
console.log(evaluate(input));

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to convert some array to string maybe some recursive function works.

I am assuming that you have given an array where first element is condition and remaining 2 are the variables in which you want to put it.

var payload = [
    'AND',
    ['<', 'var1', 'var2'],
    ['OR', ['>', 'var3', 'var4'], ['==', 'var5', 'var6']],
  ]



function convertFunction(array) {

  const condition = array[0];
  let LHS = array[1];
  let RHS = array[2];

  if(Array.isArray(LHS)){
    LHS = convertFunction(LHS);
  }

  if(Array.isArray(RHS)){
    RHS = `(${convertFunction(RHS)})`;
  }

  return `${LHS} ${condition} ${RHS}`

}

console.log(convertFunction(payload));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Not sure if the correct algorithm is used to determine if brackets are needed.

const input = [ "AND", ["<", "var1", "var2"], [ "OR", [">", "var3", "var4"], ["==", "var5", "var6"] ] ];

const strArr = (arr) => {
    const printSide = (side) => Array.isArray(side) 
      ? printBrackets(side) 
      : side;
    
    const printBrackets = (side) => Array.isArray(side[1]) && Array.isArray(side[2])
      ? `(${strArr(side)})` 
      : strArr(side);
      
    const [operator, left, right] = arr;
    
    return [printSide(left), operator, printSide(right)].join(' ');
};

console.log(strArr(input));
.as-console-wrapper{min-height: 100%!important; top: 0}

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