0

how is it possible to use an add operator within a string. I have the following function:

var point_1_x = 50;
var point_2_y = 100;

array[0].animate({path:"M, "+point_1_x+", "+point_1_y+"", 5, '<>');}

but I want to use var point_1_x and add an integer to it. How would be the correct syntax to do this.

array[0].animate({path:"M, "+point_1_x+500+", "+point_1_y+"", 5, '<>');}

does not seem to work.

Cheers

5
  • 99%: +parseInt(parseInt(point_1_x)+Number(500))+ Commented Mar 28, 2014 at 20:42
  • 1
    @Kristiyan - There is no need for parseInt, here. He is trying to perform the operation inline and already has the integers. Commented Mar 28, 2014 at 20:44
  • @ZacharyKniebel, I know. But if he make other manipulations, which are not poste, this will prevent type conflict. Commented Mar 28, 2014 at 20:46
  • @Kristiyan - That will only prevent type conflicts if he has the numbers in string form already, which is not part of the OP. Commented Mar 28, 2014 at 20:48
  • It probably doesn't work because you've misnested brackets. The } needs to be inside the ). Commented Mar 28, 2014 at 22:09

3 Answers 3

2

use brackets, then it should work:

array[0].animate({path:"M, "+ ( point_1_x+500 ) +", "+point_1_y+"", 5, '<>');
Sign up to request clarification or add additional context in comments.

Comments

1

Give the following a try:

array[0].animate({path:"M, " + (point_1_x + 500) + ", " + point_1_y, 5, '<>');

JavaScript is a string-based language, and it is not type safe. You can perform mathematical operations within a statement in which you are concatenating integers into a string by wrapping the operations in parenthesis.

Also note that you did not close the brace you opened before path:, so use the following:

array[0].animate({path:"M, " + (point_1_x + 500) + ", " + point_1_y, 5, '<>'});

1 Comment

Glad to have helped :) Happy coding!
1

Use parentheses :

array[0].animate({path:"M, "+(point_1_x+500)+", "+point_1_y+"", 5, '<>');

Comments

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.