Quin Proctor Quin Proctor - 1 month ago 15
Javascript Question

Javascript - How can I find the name of a variable instead of the value it holds

I have this function to set the movement of cubes on the canvas (random),

everyVar
is the variables that will be used and
a,b,c,d
are functions for
+,-,*,/
(sorry if its confusing). Is there a way to find the name of the variable instead of the value it holds; I have "
user.s = a(b(c(d(r, 2), e), w), q);
" and i need a way to put that in a repeating function so that the variables stay the same but the information updates, but if i put it somewhere else as it is it will have different variables. I cant just set another variable like
myVariable = q
because it will save the number not the variable itself (i need it to say user.y instead of 300 for example). Please make your answer as simple as possible as I am a beginner as you can probably tell.

function setMovement(user){
var everyVar = [user.x, user.y, user.direction, user.distance]

var q = everyVar[Math.floor(Math.random()*everyVar.length)];
var w = everyVar[Math.floor(Math.random()*everyVar.length)];
var e = everyVar[Math.floor(Math.random()*everyVar.length)];
var r = everyVar[Math.floor(Math.random()*everyVar.length)];

user.s = a(b(c(d(r, 2), e), w), q);

a = operators[Math.floor(Math.random()*operators.length)]
b = operators[Math.floor(Math.random()*operators.length)]
c = operators[Math.floor(Math.random()*operators.length)]
d = operators[Math.floor(Math.random()*operators.length)]
}

Answer

Here is some code you could use:

// Define the operators as functions
var operators = [
    function add(x, y) { return x + y },
    function sub(x, y) { return x - y },
    function mul(x, y) { return x * y },
    function div(x, y) { return x / y }
];

// Don't reference the values, but the property names
var everyVar = ['x', 'y', 'direction', 'distance'];

function setMovement(user){
    var q = everyVar[Math.floor(Math.random()*everyVar.length)];
    var w = everyVar[Math.floor(Math.random()*everyVar.length)];
    var e = everyVar[Math.floor(Math.random()*everyVar.length)];
    var r = everyVar[Math.floor(Math.random()*everyVar.length)];

    var a = operators[Math.floor(Math.random()*operators.length)]
    var b = operators[Math.floor(Math.random()*operators.length)]
    var c = operators[Math.floor(Math.random()*operators.length)]
    var d = operators[Math.floor(Math.random()*operators.length)]

    // Make s a function, so the expression is evaluated with the
    // current values of the user object properties:
    user.s = function () {
        return a(b(c(d(user[r], 2), user[e]), user[w]), user[q]);
    };
    // For debugging, you can use this extra property to see
    // which expression the "random" function evaluates:    
    user.s.description = `${a.name}(${b.name}(${c.name}(${d.name}` +
                         `(user.${r}, 2), user.${e}), user.${w}), user.${q})`;
}

// same data
var user = {
    x: 20,
    y: 10,
    direction: 1,
    distance: 5
}
// Define "random" function user.s
setMovement(user);

console.log( 'user.s() evaluates this expression:\n', user.s.description );

console.log( 'result of user.s():', user.s() );

// Change x and y and display result then:
user.x -= 5; 
user.y += 10;
console.log( 'result of user.s():', user.s() );

NB: be careful with division: when the second argument is 0, you'll get a run-time error.