-2

I have been following a guide on Udemy on Javascript. The guy who was doing helped me out alot but then i got a syntax error telling me to put in a semi colon. I put it in but then it didnt run properly. Please take a look.

function calculateMousePos(evt)
{
var rect = canvas.getBoundingClientRect();
var root = document.documentElement;
var mouseX = evt.clientX - rect.left - root.scrollLeft;
var mouseY = evt.clientY - rect.top - root.scrollTop;
return 
{
        x:mouseX,
        y:mouseY
};
}
Pang
  • 9,564
  • 146
  • 81
  • 122
D.Kippen
  • 1
  • 3

2 Answers2

2

In Javascript a return statement must be (at least partially) on the same line as your return value:

return {
  x:mouseX,
  y:mouseY
};
UncleDave
  • 6,872
  • 1
  • 26
  • 44
2

You need to move the curly baracket of the return object into the same line, because of Javascript's automatic semicolon insertion (ASI).

The error results of a blocks statement and a second label in it.

function calculateMousePos(evt) {
    var rect = canvas.getBoundingClientRect();
    var root = document.documentElement;
    var mouseX = evt.clientX - rect.left - root.scrollLeft;
    var mouseY = evt.clientY - rect.top - root.scrollTop;
    return {
        x: mouseX,
        y: mouseY
    };
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392