Click here to Skip to main content
15,899,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one input-box in my code. From input string I get operators and operand.

If user type "10 * 20 / 30 - 60 + 2" I get the value by getElementById

So now the question is, is there any way to do this array operations in order?

I'm expecting this calculation like:

10 * 20 is 200
200 / 30 is 6.66
6.66 - 60 is -53.33
-53.33 + 2 is -51.33
So -51.33 must be final answer.

I have tried this calculation by eval() function but the answer I got is wrong. I also want to display that answer in same input box, please don't give me examples with two text-boxes.

I have described my problem as well as I can. You can see my code link which is in comment box.

Another way to explain e.g- ["10 * 2"-> answer-> "answer / 30"-> answer -> "answer - 60" -> answer -> "answer + 2" -> answer]


What I have tried:

my code link :- jsfiddle.net/archan/z3mqnxz7
Posted
Updated 2-Mar-17 0:05am
Comments
Patrice T 2-Mar-17 3:26am    
You only forgot to tell us the result you get with eval.

1 solution

JavaScript
<pre> function getResult(exp) {
            exp = '1 * ' + exp;
            var parts = exp.split(' ');
            var result, num = [], exp = []; var formu = [];

            for (var i = 0; i < parts.length; i++) {
                var item = parts[i].trim();
                if (isNaN(item))
                    exp.push(item);
                else
                    num.push(item);
            }
            for (var i = 0; i < num.length + 1; i++) {
                var expr = exp[i - 1] == undefined ? '' : exp[i - 1];
                var numr = num[i] == undefined ? '' : num[i];
                var str = expr + numr;
                if (i == 0)
                    str = '1'
                result = result == undefined ? '' : result;
                result = eval(result + str);
            }
            return result;
        }

        console.log(getResult(  "10 * 20 / 30 - 60 + 2"))  //- 51.3333
 
Share this answer
 
Comments
Member 13010669 2-Mar-17 3:38am    
hey thanks for the help. but in you program is it possible to get value from input string and pass that value in getResult() like you initialize so without initialization can i do that? i want to this same program like you have done great job but only i want to do that without initialization.
Karthik_Mahalingam 2-Mar-17 3:42am    
yes possible, just call the function..
Member 13010669 2-Mar-17 3:39am    
like i'm getting value like e.g- var demo= document.getElementById('new').value; and that demo value. suppose user type "10/6*8+7-9" so this value is get initialize in getresult("") like you did so how i can do like this?
Karthik_Mahalingam 2-Mar-17 3:46am    
if i only do everything, then what will you do,
bro just go through the code and try..
if you couldnt after trying then i will help..
Member 13010669 2-Mar-17 4:46am    
Sorry Bro!! not getting how to do that. i have tried so hard also go through all program as you said but still not getting how to pass that string value in getResult().
I have tried like this, created another and in that function i have stored input value now that value trying to pass in getresult() but still that dose not work.
function equ_click()
{
var ans = document.getElementById('new').value;
}
and outside the function definition of getresult() i have declared,
getResult(ans);

please help me where i really don't know where i made mistake . and thanks for your awesome support

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900