Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Equations calculator - help

    Hey guys. I'm working on a program that can calculate your equations. I'm not finish yet but as you can see here, I want it to print out the 'c' variable and the 'b' variable via an alert. So what's wrong with it? Later I will make css and finish the code...

    Code:
    HTML:
    
    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
            <link/>
            <script></script>
        </head>
        <body>
        <script type="text/javascript" src="script.js"></script>
        </body>
    </html>
    ----------------------------------------------

    Javascript:

    Code:
    var ligning = prompt("Indtast din ligning her:");
    
    var calculator = function() {
        for(i = 0; j=true; i++) {
            var k = i;
            var a = ligning.substring(k,1+k);
            if (a === "=") {
                j = false;
                var b = ligning.substring(0, i);
                var c = ligning.substring(i+1, 200);
                alert("end.")
        }
            else {j=true;}
        }
    };
    alert(ligning);
    alert(c);
    alert(b);
    Last edited by Potices; 01-09-2013 at 04:10 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    You are closing the for loop before anything else is processed, so it will run indefinitely, as j will always be true.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh your right. I changed it, but It still doesn't work at all. I updated the thread, so the 'new' code is at the very top.

    I did also add a new alert. In the event; 'if (a === "=")' I added the alert 'end' just so I could see if it could find the "=". But it couldn't, so my question is why, and how can I fix it?

  • #4
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    A simpler variant :
    Code:
    var k=0;
    while (ligning.substring(k) && ligning.substring(k,k+1)!=='=') k++;
       
       if (ligning.substring(k,k+1)==='=')  {// We find the sign = 
          var b = ligning.substring(0,k);
          var c = ligning.substring(k+1, 200);
       }
       else alert('This entry is not an equation');
    But, an other sign = is possible in c !!!

    Incidentally, there is no need to make a loop to find and cut a string on a sign =

    Code:
    var arrEqt = ligning.split('='); // give immediately arrEqt[0] and arrEqt[1] if the length of arrEqt is two !
    Please use the Go Advanced and Code buttons...
    Last edited by 007julien; 01-09-2013 at 04:25 PM. Reason: complement

  • #5
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much 007julien! I'm new at Javascript and I will use the .split method instead. Thank you very much!


  •  

    Tags for this Thread

    Posting Permissions

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