0

I have a page where user can select and define their own formula for calculating "WORKLOAD" of lecturers

For example, (Jobtypevalue + Creditpoint) * Number of Students

I store this defined formula by the user in database.

Now, I want use this formula in another page which are fields (textbox) through which user can enter any number and at the bottom I need to calculate by using values entered by user based on the predefined formula from the database

If user enters Jobtypevalue = 7, Creditpoint = 3 and Number of students = 10 Then, at the bottom of page, I need calculate [(7+3)*10] and display "Workload = 100"

How can I do this using javascript ? Since, I have the formula on pageload from database and values are entered by user later, thought it is better to calculate the workload using javescript(client side)

1
  • Fixed number of operations? i.e first add var1,var2 and then multiply with var3. Or number and type of operations are variable? Commented Oct 22, 2012 at 9:05

1 Answer 1

1

You can try like this but your variable name should follow proper naming convention

//values in variables
var Jobtypevalue =7;
var Creditpoint = 3;
var Number_of_Students=10;

//use eval to evalute.
alert(eval("(Jobtypevalue + Creditpoint) * Number_of_Students"));

You can check this DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

Do not think this is all what needed in question still +1.. because whatever would be the right solution will need eval
Yes, but how do I get the formula "Jobtypevalue + Creditpoint) * Number_of_Students" I can place the user defined formula stored in database, on a textbox and how do I use the formula from text box and calculate the values based on the formula.
@Raj you are getting it from db so you can store it in a hidden field or you can store it in a js variable like var formula= '<%= your db formula field value %>';
If I have, textbox_formula.value = (Jobtypevalue + Creditpoint) * Number_of_Students alert(eval(texbox.value)
@Raj, yes but need to create variables and assign values before calling this eval function.

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.