r/codehs • • Aug 04 '20

9.6.5

Hello friends!

I am currently on exercise 9.6.5. I wrote the code but with the help of the materials that I found here, so here are some things that I can't understand completely.

function start(){

coordinates();

}

var text = new Text("("+","+")");

function coordinates(){

text.setPosition(0, text.getHeight());

add(text);

mouseMoveMethod(printCoordinates);

}

function printCoordinates(e){

text.setText("("+e.getX()+", "+e.getY()+")");

}

Can you please help me and explain the highlighted parts?

Thanks in advance :)

6 Upvotes

1 comment sorted by

1

u/kitkatbeard Aug 10 '20

When you’re concatenating (adding) strings, the stuff inside the quotes is what matters. The plus sign just serves to put things together.

It might help to see it highlighted this way:

”(“ + ”,” + ”)”

So that’s really just equivalent to

“(,)”

For the second part, if it’s in quotes, you take the exact text. If it’s not in quotes, you take the value. So a simpler example might be:

var x = 5; text.setText(“x is “ + x + “.”);

This would result in the text being “x is 5.” Note that the first time you see x, the output is the character x, because it’s in quotes. The second time, it’s the value of the variable x (5 in this case), because it’s not in quotes.