0

So I was trying to create a function that would put the value of i and j in a string as if it was a function. Lets say that we have a string = "i+j"

what i want is:

sum = sum + i + j

but currently what happens is

sum = sum + "i + j"

anyone got a solution for this?

3
  • 1
    Use evil. Wait, I typed eval, but somehow it changed... Commented Aug 2, 2013 at 7:05
  • In a string or get variables from string and add it to sum variable? Commented Aug 2, 2013 at 7:05
  • possible duplicate of Writing a function that "solves" an equation Commented Aug 2, 2013 at 7:05

2 Answers 2

2
var i = 5,
    j = 10,
    string = "i+j",
    sum = 100;

sum = sum + eval(string);

console.log(sum); // output 115
Sign up to request clarification or add additional context in comments.

Comments

-1

use parseInt() :

sum = sum + parseInt(i) + parseInt(j)

1 Comment

Please read the question carefully. The OP has the string "i + j" and wants to evaluate it. parseInt does not help here.

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.