How would I do this in Javascript?

Given a string of expressions (only variables, +, and -, ignore whitespace) and a set of variable/value pairs (i.e. a=1, b=7, c=3, d=14) return the result of the expression ("a + b+c -d" would be -3).
brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
How about?

var s = "a + b+c -d";
var v = { a: 1, b: 7, c: 3, d: 14 };
var variables = Object.keys(v);
var expression = s;

for (var i = 0; i < variables.length; i++) {
    var replacementValue = v[variables[i]];
    var variable = variables[i];
    
    expression = expression.replace(variable, replacementValue);
}

var result = eval(expression);

alert(result);

Open in new window


In action: http://jsfiddle.net/4Y24D/2/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
No points for this, please.

You really, really want to get the basics right before you try to move on.  Trust me -- I've taught this stuff at the postgraduate level and I've seen the difference in accuracy and productivity between those who engaged in structured learning, and those who tried to learn from trial and error and a handful of examples.  Your work will be at least an order of magnitude better and faster if you take a little time to learn JavaScript from the ground up!  If you understand why something works you can apply it to other problems.
0
brucegustPHP DeveloperAuthor Commented:
I appreciate that Ray, but I'm in a bind because I've been asked to field some questions pertaining to Javascript and I told them that, in this instance, it's nothing more than a gauge of my being able to google than it is to code. So, I'll have to engage in a proper study after Monday morning.

I don't want you to hear me as being overly casual, I'm just in a place where for the next 48 hours it's not so much about learning as it is producing. Hence the frantic search for answers more so than theory.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

brucegustPHP DeveloperAuthor Commented:
kaufmed, I'm using exactly what you gave me and I see it firing correctly on your link, but it's not working on my box.

Here's my code in its entirety. Something's going south, but I don't know what.

<html>
<head>
<title>Factorial Function Which Uses Recursion</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>

<body>

Here's an implementation of a factorial function which uses recursion. (i.e. factorial(7) -> 5040)
<br>
<br>
<script language="javascript">
var s = "a + b+c -d";
var v = { a: 1, b: 7, c: 3, d: 14 };
var variables = Object.keys(v);
var expression = s;

for (var i = 0; i < variables.length; i++) {
    var replacementValue = v[variables[i]];
    var variable = variables[i];
    
    expression = expression.replace(variable, replacementValue);
}

var result = eval(expression);

alert(result);

</script>

</body>
</html>

Open in new window

0
käµfm³d 👽Commented:
Dunno. I copy/pasted it exactly from your snippet above, and it works fine.
Untitled.png
0
Ray PaseurCommented:
Copied and pasted the code snippet from the comment above here:
http://iconoun.com/demo/temp_brucegust.php

It does what it is supposed to do, however the comments do not seem to make sense.  The code appears intended to evaluate some arithmetic expression, but the comments make reference to factorial?
0
brucegustPHP DeveloperAuthor Commented:
And that's my fault, Ray. The comments were left over from a different exercise. When I ran the code on a different server, it worked fine. In some instances, my workstation is antiquated and I'm thinking that may have been the culprit more so than the code.

It's all good.

Thanks for your time.
0
Ray PaseurCommented:
Agreed - the code looked right and worked correctly on two browsers I tested.  Maybe worth an update to the browsers if you can do that.  Technology is always advancing!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.