Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Equations calculator - help

1. ## 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>
<title></title>
<script></script>
<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);
}
else {j=true;}
}
};

2. You are closing the for loop before anything else is processed, so it will run indefinitely, as j will always be true.

3. 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. 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 !`

5. Thank you very much 007julien! I'm new at Javascript and I will use the .split method instead. Thank you very much!