Results 1 to 6 of 6

Thread: Javascript maths

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Javascript maths

    Index.html

    HTML Code:
    <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

    HTML Code:
    function basicmath(a,b,c)
    {
    return  a b c ;
    }
    Could someone please tell me what I'm doing wrong?
    Last edited by keyboard; 02-04-2012 at 06:13 AM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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...
    Last edited by djr33; 02-03-2012 at 06:57 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    So I can't pass math operators to a function???

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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()...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <!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:

    Code:
    function basicmath(a, b, c){
    	return [a, b, c, '= '].join(' ') + eval(a + b + c);
    }
    Last edited by jscheuer1; 02-03-2012 at 07:40 AM. Reason: add variation
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •