Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2013, 03:19 PM   PM User | #1
Potices
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Potices is an unknown quantity at this point
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..
Potices is offline   Reply With Quote
Old 01-09-2013, 03:55 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 945
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 01-09-2013, 04:09 PM   PM User | #3
Potices
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Potices is an unknown quantity at this point
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?
Potices is offline   Reply With Quote
Old 01-09-2013, 04:10 PM   PM User | #4
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
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
007julien is offline   Reply With Quote
Old 01-09-2013, 05:22 PM   PM User | #5
Potices
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Potices is an unknown quantity at this point
Thank you very much 007julien! I'm new at Javascript and I will use the .split method instead. Thank you very much!
Potices is offline   Reply With Quote
Reply

Bookmarks

Tags
calculator, equation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:57 PM.


Advertisement
Log in to turn off these ads.