1

I was wondering how i could achieve the above with the code i have below. So for example, when the data is joined i would have in an index in the new array combinedresults something like [2.62, 460]. Both functions below are called through an event listener when the user clicks a button. Any help would be much appreciated, thanks.

var mouseDistance = new Array();
var timers = new Array();
var combinedresults = new Array();

//THIS FUNCTION CALCULATES THE DISTANCE MOVED
function printMousePos(e) {
    var lastSeenAt = {
        x: null,
        y: null
    };
    var cursorX = e.clientX;
    var cursorY = e.clientY;

    var math = Math.round(Math.sqrt(Math.pow(lastSeenAt.y - cursorY, 2) +
        Math.pow(lastSeenAt.x - cursorX, 2)));
    mouseDistance.push(math);
}

function stopCount() {
    clearTimeout(t);
    timer_is_on = 0;
    timers.push(t);
}
4
  • var lastSeenAt = {x: null, y: null}; is causing the trouble I guess. Move this out of that function. Otherwise you just try to compute Math.pow(null - somenumer, 2). Commented Oct 29, 2015 at 13:53
  • Thanks, though actually im wondering how to go about implementing the logic above in the question. Commented Oct 29, 2015 at 13:55
  • Where's prevX, prevY, totalTravelled then, can't see where they're used in your code. Commented Oct 29, 2015 at 13:58
  • My bad, those aren't supposed to be in there. Commented Oct 29, 2015 at 14:01

1 Answer 1

1

You can move that lastSeenAt's init out of the function. And assign new value at the end of the function.

To get combinedresults, use the shorter of mouseDistance and timers, and push the data into combinedresults should work.

var mouseDistance = new Array();
var timers = new Array();
var combinedresults = new Array();
// Init with both is null.
var lastSeenAt = {
  x: null,
  y: null
};

//THIS FUNCTION CALCULATES THE DISTANCE MOVED
function printMousePos(e) {
  var cursorX = e.clientX;
  var cursorY = e.clientY;

  // Don't calculate when x, y is null, which is the first time.
  // Or you can give lastSeen some other initValue rather than (null, null).
  if (lastSeenAt.x !== null) {
    var math = Math.round(Math.sqrt(Math.pow(lastSeenAt.y - cursorY, 2) +
        Math.pow(lastSeenAt.x - cursorX, 2)));
    mouseDistance.push(math);      
  }

  // Keep the x,y value.
  lastSeenAt.x = cursorX;
  lastSeenAt.y = cursorY;
}

function stopCount() {
    clearTimeout(t);
    timer_is_on = 0;
    timers.push(t);
}

// get combinedresults 
function getCombinedResult() {
  // Get the shorter length.
  var length = Math.min(mouseDistance.length, timers.length);
  var i;

  // 
  for (i = 0; i < length; ++i) {
    combinedresults[i] = [timers[i], mouseDistance[i]];
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for that. Though what I really need is how i can merge the data in one index from the timers array plus the data in one index from the mouseDistance array into one index in one new array called combinedresults.
You mean, you want your function also do combinedresults[n] = timers[n] * mouseDistance[n] ?
Yes, something like if the timers array has a value 2.62, and the mousedistance array has a value of 500, I want to put both those data into a single array, so something like [2.62, 500], thanks!
Is that be done on another function, or you want it to be done in printMousePos? Also, can we ensure that timers and mouseDistance has same length, or combinedresults's length is equal to either of them?
In another function. Yup, combinedresults' length will depend on the length of both the arrays timers, and mouseDistance, thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.