I am trying to make a tool to create MySQL query with javascript. So I have separate input boxes to fill the operator and operands. The example array looks like this.
var array = [ "OR", ["<", "col1", "col2"], [ "AND", ["==", "col3", "col4"], ["!=", "colx", "coly"]]];
var array2 = [ "OR", ["<", "col1", "col2"], [ "AND", ["==", "col3", "col4"], ["!=", "colx", "coly"], ["<", "colo", "othercol"]]];
the expected out put is
"col1 < col2 OR (col3 == col4 AND colx != coly)"
"col1 < col2 OR (col3 == col4 AND colx != coly AND colo < othercol)"
What I am trying is something like this.
function createString(array) {
var string = '';
for(var i = 0; i < array.length; i++) {
if(Array.isArray(array[i])) {
//console.log(array[i]);
if(array[i].length == 3) {
var operator = array[i][0];
var variable1 = array[i][1];
var variable2 = array[i][2];
string+= variable1+operator+variable2;
}else {
//console.log('length is '+array[i].length);
}
//is array
}else {
//string
//console.log('it is string '+ array[i]);
string+=array[i];
}
}
return string;
}
But I don't think I am trying the right algorithm. I am still trying this and I will keep my question updated with each try.
thanks in advance