PDA

View Full Version : Resolved Javascript maths



keyboard
02-03-2012, 05:44 AM
Index.html



<html>
<head>
<script language="JavaScript" src="function.js"></script>
</head>
<body>
<input type="button" onclick="
alert(basicmath(4,+,3));" value="click me">
</body>
</html>


function.js



function basicmath(a,b,c)
{
return a b c ;
}


Could someone please tell me what I'm doing wrong?

djr33
02-03-2012, 06:51 AM
I don't see why that would work at all. Most programming languages don't allow you to mix operators and variables. So '+' is simply not a value that you can put into a function, except as a string (and it wouldn't do math).
Of course you could try to do this with more options (such as using an if statement to check if '+' is the value), but not as you're doing it.

You could always use eval(), and that would work, but it's also messy and open to user (typo?) errors.


More specifically, you should have two errors in that code:
1. + can't be in that position. That's something like saying var x = if (so you can use the variable x to create an if statement later-- x (y==z)... honestly, it just makes no sense to me). For + to be in that position, it would need to be in a string (surrounded by quotes).
2. a b c is a syntax error. You can't just put two (or three) variables next to each other. It's not going to evaluate them, it's just going to get confused. That's like telling it "var x = 1 2 3 4".

Open up your "error console" in your browser (or whatever the equivalent is) and check what the latest error messages were.


But again, if you really want, there's always eval().



As a tangent, it would be somewhat amusing playing with a dynamically (and potentially self-) written language so that you could actually do things like this. But it would probably get too messy to deal with fast and maybe end up in some odd endless loop or something-- not a good idea, really. This also borders on what artificial intelligence would be like (since you can think of humans as constantly "reprogramming" ourselves when we do new things)... another topic...

keyboard
02-03-2012, 06:55 AM
So I can't pass math operators to a function???

djr33
02-03-2012, 06:58 AM
I edited my post a couple times. Check that. And, no, you can't.

The reason is pretty simple: the + isn't a "thing"-- it's a command. So it makes about as little sense as telling someone "go read a book", asking them to hold onto those words for later, then at a later time (maybe when an alarm clock rings) to use those words. Odd. If that's even remotely possible, it's basically the same thing as eval()...

jscheuer1
02-03-2012, 07:33 AM
There's a function in javascript specifically for, among other things, doing math, eval(). It's use is generally discouraged because it takes/expects a string and converts it into a function or expression as best it can. As a result it's prone to errors of unexpected type conversion and/or incorrect quoting. That said, this works in Firefox at least, probably all others:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function basicmath(a, b, c){
return eval(a + b + c);
}
</script>
</head>
<body>
<input type="button" onclick="alert(basicmath(4, '+', 3));" value="4 plus 3"><br>
<input type="button" onclick="alert(basicmath(4, '/', 3));" value="4 over 3"><br>
<input type="button" onclick="alert(basicmath(4, '-', 3));" value="4 minus 3"><br>
<input type="button" onclick="alert(basicmath(4, '*', 3));" value="4 times 3"><br>
</body>
</html>

For a little more fun, try this variation:


function basicmath(a, b, c){
return [a, b, c, '= '].join(' ') + eval(a + b + c);
}

keyboard
02-04-2012, 06:12 AM
Thanks everyone! :)