简体   繁体   中英

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 inte.net 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

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

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

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 res = [printSide(left), operator, printSide(right)].join(' '); }; console.log(strArr(input));
 .as-console-wrapper{min-height: 100%;important: top: 0}

You may use "eval" in order to evaluate the values of the operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM