math - javascript logic returning strange results -


i have grid trying program cursor onto, there central point in grid , trying make cursor able move x amount of squares central point, here code have

var makecursor = function (axis, operation) {     oppositeaxis = axis === 'y' ? 'x' : 'y';      // app.cursor contains x , y coordinates of cursor     app.cursor[axis] =       // calcualte difference between current cursor location , origin     math.abs( app.cursor[axis] - origin[axis] ) +      // calculate difference between opposite axis ,    // origin , add previous calculation     math.abs( app.cursor[oppositaxis] - origin[oppositeaxis] )     // add operation performed ( movement of cursor, 1 or -1 )   + operation     // if sum of x, y , current operation greater allowed   // movement, make "app.cursor[axis]" equal (dont move) ,    // otherwise make "app.cursor[axis]" equal plus operation ( move )  > origin.movement ? app.cursor[axis] : app.cursor[axis] + operation; } 

"operation" either 1 or -1, directional

"origin.movement" number of squares origin can move cursor.

my hopes / expected behavior square on grid, can move many squares specified in "origin.movement" variable. returning strange numbers when print out results, , doesn't calculate locations correctly, i.e. origin should zero, instead one, or two, depending on previous movements, many other anomalies haven't been able understand. problem appreciated, thanks!

you need put parens around expression you're testing > origin.movement in order use expression's result there; without them, expression broken earlier:

var makecursor = function (axis, operation) {     var oppositeaxis = axis === 'y' ? 'x' : 'y';      app.cursor[axis] = (         // calculate difference between current cursor location , origin         math.abs( app.cursor[axis] - origin[axis] ) +           // calculate difference between opposite axis ,         // origin , add previous calculation         math.abs( app.cursor[oppositeaxis] - origin[oppositeaxis] ) +          // add operation performed ( movement of cursor, 1 or -1 )         operation          // if sum of x, y , current operation greater allowed         // movement, make "app.cursor[axis]" equal (dont move) ,          // otherwise make "app.cursor[axis]" equal plus operation ( move )     ) > operation ? app.cursor[axis] : app.cursor[axis] + operation; } 

but, wouldn't on 1 long line assignment-to-self anyway, i'd break clarity , ease of debugging, , use if:

so:

var makecursor = function (axis, operation) {     var oppositeaxis = axis === 'y' ? 'x' : 'y';      var movement =          // calculate difference between current cursor location , origin         math.abs( app.cursor[axis] - origin[axis] ) +           // calculate difference between opposite axis ,         // origin , add previous calculation         math.abs( app.cursor[oppositeaxis] - origin[oppositeaxis] ) +          // add operation performed ( movement of cursor, 1 or -1 )         operation;      // if sum of x, y , current operation less or equal     // allowed movement, add operation `app.cursor[axis]`     if (movement <= origin.movement) {         app.cursor[axis] += operation;     } } 

side note: code falling prey the horror of implicit globals not declaring oppositeaxis variable (there typo using later). i've fixed typo , fixed implicit global adding var above.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -