Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 86
  • Last Modified:

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).
0
brucegust
Asked:
brucegust
  • 3
  • 3
  • 2
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now