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

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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