Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!