Having trouble with the syntax of a string in Javascript

1.6k views Asked by At
fill(225, 0, 225);
var draw = function() {
    background(255, 255, 255);
    ellipse(mouseX, mouseY, 12, 12); 
    var label = mouseX + "36", "45" + mouseY;
    text(label, mouseX, mouseY);
};

I'm creating a small mouse tracker program that calls the coordinates of the mouse pointer just below and to the right of a small circle, using Khan Academy, it's one of their challenges. In particular I'm getting errors when creating the string where I'm attempting to store mouseX and Y along with some coordinates in to the variable 'label'.

3

There are 3 answers

1
Alex Wayne On
var label = mouseX + "36", "45" + mouseY;

You have some funky syntax there, that's not a proper way to use a comma when creating strings. It's not obvious what you really want, but I'm betting something like this:

var label = "mouseX:" + mouseX + ", mouseY" + mouseY;

Which would save a string like "mouseX:123, mouseY:456" to the var label.

0
user3599708 On

This is how i got it to work!

fill(255, 0, 255);

var draw = function() {
    background(255, 255, 255);
    ellipse(mouseX, mouseY, 12, 12);
    var label = mouseX + " , " + mouseY;
    text(label, mouseX, mouseY);
};
0
360gamegod On

You do not need var next to the draw = function(), and instead of putting the numbers in the quotes, put a space between the quotes, also make the label variable outside of the draw function, so that your code looks like this :

var label;
draw = function() {
    background(255, 255, 255);
    fill(225, 0, 225);
    ellipse(mouseX, mouseY, 12, 12); 
    label = "    " + mouseX + "   " + mouseY;
    fill(0,0,0);
    text(label, mouseX, mouseY);
};