简体   繁体   中英

Variable is not changing value

So, I'm designing a code that will enable the user to create pseudo-custom operations that one can use under a special eval() function (as JavaScript is not an extendable language). My problem is that only the first variable created seems to register and be evaluated.

I am posting here a large snippet of the code.

var CMD = function(){
    var objs = gAO() /* gets all of the objects */;
    // testing for other instances of the CMD object.
    this .bool = 0;
    for(obj in objs) this .bool ^= !objs[obj]["_aqz39"] // boolean
    if(this .bool){
        // DEFINING VARS
        this .objs = objs;
        this["_aqz39"] = true;
        this .ops = []; this .eqs = [];
    }
}
{ /* init */
    var cmd = new CMD();
}

// USER INPUT FOR CREATING 'NEW VARIABLES'
var Operator = function(op,input){
    // SYNTAX: "<operator>","x <operator> y = <result, using 'x' and 'y'>"
    // EXAMPLE: "#","x # y = 3 * x - y"
    this .op =  op;
    this .eq = input.split("=")[1].trim();
}


// FUNCTION FOR ACTIVATING THE VARIABLE TO BE
// ...RECOGNIZED BY THE CMD's 'EVAL' FUNCTION
activate = function(ind){
    cmd.ops.push(ind.op);
    cmd.eqs.push(ind.eq);
}

CMD.prototype.eval = function(equ){
    // DECLARING VARS
    var t = cmd,oper,equation,x,y,i=0;
    // LOOPS THROUGH ALL OF THE CHILDREN OF cmd.ops
    while (i < t["ops"].length){
        // CHECKS TO SEE IF THE INPUT CONTAINS THE SYMBOL
        if(equ.search(oper) !== -1){
                // the operator
                oper = t["ops"][i];
                // the equation
                equation = t["eqs"][i];
                // from the first index to the beginning of the operator
                x = equ.slice(0,equ.search(oper)).trim(),
                // from right after the operator to the end of the thing
                y = equ.slice(equ.search(oper)+1,equ.length).trim();
                /* INFORMATION LOGGING */
                console.log({x:x,y:y,oper:oper,equation:equation,i:i,t:t,bool: equ.search(oper),len:t["ops"].length})
            // RESULT
            return eval(eval(equation));
        }
        // INCREMENTS 'i'
        i++;
    }
    // ELSE
    return false;
}

Testing #1

var hash = new Operator("#","x # y = 3 * x - y");
var dash = new Operator("q","x q y = y");

activate(dash);
activate(hash);

console.log(cmd.eval("3 q -2")); // RETURNS -2
console.log(cmd.eval("3 # -2")); // RETURNS NOTHING

Testing #2

var hash = new Operator("#","x # y = 3 * x - y");
var dash = new Operator("q","x q y = y");

activate(hash); // HASH IS CALLED FIRST THIS TIME
activate(dash);

console.log(cmd.eval("3 q -2")); // RETURNS NaN
console.log(cmd.eval("3 # -2")); // RETURNS 11

I've been troubleshooting this thing for about an hour, and I have no idea what's going wrong. Help is highly appreciated.

Here you are using the variable oper before you have assigned anything to it:

if(equ.search(oper) !== -1){
  oper = t["ops"][i];

The undefined value will be converted into an empty regular expression, so it will always return a match, that's why the first operator works. In the next iteration the variable will be assigned the wrong operator.

Assign the operator to it before using it to look for the operator:

oper = t["ops"][i];
if(equ.search(oper) !== -1){

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