JavaScript calculation problem!!!!

willemvo

New Member
Messages
19
Reaction score
0
Points
0
i dunno what i did wrong,,, can anybody help me?
Code:
<html>
<head><title>TEST</title>
<script type="text/javascript" language="JavaScript1.3">
/*i'm dutch so maybe there will be some terms you don't get!!*/
function plusje(form) {
 document.getElementById("number").disabled=true;
 document.getElementById("sign").value='+';
}

function minnetje(form) {
 document.getElementById("number").disabled=true;
 document.getElementById("sign").value='-';
}

function keertje(form) {
 document.getElementById("number").disabled=true;
 document.getElementById("sign").value='X';
}

function deeltje(form) {
 document.getElementById("number").disabled=true;
 document.getElementById("sign").value='/';
}

function resetje(form) {
 document.getElementById("number").disabled=false;
 document.getElementById("number").value='1';
 document.getElementById("sign").value='';
 document.getElementById("number2").disabled=false;
 document.getElementById("number2").value='2';
}

function bereken(form) {
 var sign = document.getElementById("sign").value;
 
 if (sign == '+') 
 {
  erbij();
 }
 else if (sign == '-')
 {
  eraf()
 }
 else if (sign == 'X')
 {
  keren()
 }
 else if (sign == '/')
 {
  delen()
 }
 else
 {
  alert ("Fill in a sign!!")
 }
}

function erbij() {
 var number1 = document.getElementById("number").value;
 var number2 = document.getElementById("number2").value;
 var answer = number1 + number2;
 var result = document.getElementById("result");
 result.innerHTML = '';
 result.innerHTML += answer; 
 if (answer > 100000 || answer < -100000)
 {
 alert ("ERROR: number is too big or too small!!");
 }
}

function eraf() {
 var number1 = document.getElementById("number").value;
 var number2 = document.getElementById("number2").value;
 var answer = number1 - number2;
 var result = document.getElementById("result");
 result.innerHTML = '';
 result.innerHTML += answer; 
 if (answer > 100000 || answer < -100000)
 {
 alert ("ERROR: number is too big or too small!!");
 }
}

function keren() {
 var number1 = document.getElementById("number").value;
 var number2 = document.getElementById("number2").value;
 var answer = number1 * number2;
 var result = document.getElementById("result");
 result.innerHTML = '';
 result.innerHTML += answer; 
 if (answer > 100000 || answer < -100000)
 {
 alert ("ERROR: number is too small or too big!!");
 }
}

function delen() {
 var number1 = document.getElementById("number").value;
 var number2 = document.getElementById("number2").value;
 var answer = number1 / number2;
 var result = document.getElementById("result");
 result.innerHTML = '';
 result.innerHTML += answer; 
 if (answer > 100000 || anser < -100000)
 {
 alert ("ERROR: number is too small or too small!!!!");
 }
}
</script>
</head>
<body>
<form name="form">
<input type="text" value="1" name="number1" id="number"  />
<input type="text" size="1" value="" id="sign" style="border:0;" name="sign" readonly />
<input type="text" value="1" name="number2" id="number2"  /> = <div id="result"></div>
<br>
<hr />
<input type="button" value="+" name="plus" onClick="plusje(this.form);" />
<input type="button" value="-" name="min" onClick="minnetje(this.form)" />
<input type="button" value="X" name="product" onClick="keertje(this.form)" />
<input type="button" value="/" name="divide" onClick="deeltje(this.form)" />
<br>
<hr />
<input type="button" value="OPNIEUW" name="opnieuw" id="opnieuw" onClick="resetje(this.form);" />
<br>
<hr />
<input type="button" value="bereken" name="bereken" onClick="bereken(this.form)" />
</form>
</body>
</html>
THANKS A LOT
 

VPmase

New Member
Messages
914
Reaction score
1
Points
0
I'm sorry but that is such a useless post.

Give me a minute and I'll see if I can find anything wrong.

Also, its recommended you don't use variables that are preset like form and etc.

Edit-
I couldn't find the problem in your code before my patience ran out so I made a similar calculator:

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<script type="text/javascript">
var cureq = "";
var numbers = new Array();
var signs = new Array();
var countnums = 0;
var eqlimit = 5;

function addEquation(sign, aform){
	if(!isNaN(aform.numberA.value) && aform.numberA.value.length > 0){
		switch(sign){
			case "+":
				numbers[countnums] = parseInt(aform.numberA.value);
				signs[countnums] = "" + sign + "";
			break;
			case "-":
				numbers[countnums] = parseInt(aform.numberA.value);
				signs[countnums] = "" + sign + "";
			break;
			case "/":
				numbers[countnums] = parseInt(aform.numberA.value);
				signs[countnums] = "" + sign + "";
			break;
			case "*":
				numbers[countnums] = parseInt(aform.numberA.value);
				signs[countnums] = "" + sign + "";
			break;
		}
		countnums++;
		if(countnums >= eqlimit){
			aform.add.disabled = true;
			aform.subtract.disabled = true;
			aform.divide.disabled = true;
			aform.multiply.disabled = true;
			cureq += numbers[countnums-1] + "";
		} else {
			cureq += numbers[countnums-1] + " " + signs[countnums-1] + " ";
			aform.numberA.value = "";
			aform.numberA.focus();
		}
		document.getElementById('equation').innerHTML = "Current equation: " + cureq;
	} else {
		alert("Please enter a real number!");
	}
}

function resetForm(aform){
	for(i=0;i<countnums;i++){
		numbers[countnums] = 0;
		signs[countnums] = "";
	}
	aform.add.disabled = false;
	aform.subtract.disabled = false;
	aform.divide.disabled = false;
	aform.multiply.disabled = false;
	countnums = 0;
	cureq = "";
	document.getElementById('equation').innerHTML = "Current equation: No Equation";
	document.getElementById('theanswer').innerHTML = "Solution will be presented here!";
}

function findSolution(){
	var answer = 0;
	for(i=0;i<countnums;i++){
		switch(signs[i]){
			case "+":
				answer += numbers[i];
			break;
			case "-":
				answer -= numbers[i];
			break;
			case "/":
				answer /= numbers[i];
			break;
			case "*":
				answer *= numbers[i];
			break;
		}
	}
	document.getElementById('theanswer').innerHTML = "Solution: " + answer;
}
</script>
<div id="equation" style="font-size:24px;">Current equation: No Equation</div>
<form name="mathform" action="" id="mathform">
<label>Add a number:<input type="text" width="100px" name="numberA" id="numberA" /></label><br />
<input type="button" name="add" id="add" value="+" onclick="addEquation('+', this.form);" />
<input type="button" name="subtract" id="subtract" value="-" onclick="addEquation('-', this.form);" />
<input type="button" name="divide" id="divide" value="/" onclick="addEquation('/', this.form);" />
<input type="button" name="multiply" id="multiply" value="*" onclick="addEquation('*', this.form);" /><br />
<input type="button" name="solve" value="Solve Equation" onclick="findSolution();" /><br />
<input type="reset" name="clear" value="Clear Equation" onclick="resetForm(this.form);" />
</form>
<div id="theanswer">Solution will be presented here!</div>
</body>
</html>
 
Last edited:

willemvo

New Member
Messages
19
Reaction score
0
Points
0
Thanx but when i try to solve eg 8*8... it doesn't work...
so i tried this but it won't work yet...
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<script type="text/javascript">
var cureq = "";
var getals = new Array();
var signs = new Array();
var countnums = 0;
var eqlimit = 5;

function erbijberekening(sign, aform){
	if(!isNaN(aform.getalA.value) && aform.getalA.value.length > 0){
		switch(sign){
			case "+":
				getals[countnums] = parseInt(aform.getalA.value);
				signs[countnums] = "" + sign + "";
			break;
			case "-":
				getals[countnums] = parseInt(aform.getalA.value);
				signs[countnums] = "" + sign + "";
			break;
			case "/":
				getals[countnums] = parseInt(aform.getalA.value);
				signs[countnums] = "" + sign + "";
			break;
			case "*":
				getals[countnums] = parseInt(aform.getalA.value);
				signs[countnums] = "" + sign + "";
			break;
			case "=":
				getals[countnums] = parseInt(aform.getalA.value);
				signs[countnums] = "" + sign + "";
			break;
		}
		countnums++;
		if(countnums >= eqlimit){
			aform.erbij.disabled = true;
			aform.eraf.disabled = true;
			aform.delen.disabled = true;
			aform.keren.disabled = true;
			cureq += getals[countnums-1] + "";
		} else {
			cureq += getals[countnums-1] + " " + signs[countnums-1] + " ";
			aform.getalA.value = "";
			aform.getalA.focus();
		}
		document.getElementById('berekening').innerHTML = "Current berekening: " + cureq;
	} else {
		alert("Please enter a real getal!");
	}
}

function resetForm(aform){
	for(i=0;i<countnums;i++){
		getals[countnums] = 0;
		signs[countnums] = "";
	}
	aform.erbij.disabled = false;
	aform.eraf.disabled = false;
	aform.delen.disabled = false;
	aform.keren.disabled = false;
	countnums = 0;
	cureq = "";
	document.getElementById('berekening').innerHTML = "Current berekening: No berekening";
	document.getElementById('theanswer').innerHTML = "Antwoord will be presented here!";
}

function findAntwoord(){
	var answer = 0;
	for(i=0;i<countnums;i++){
		switch(signs[i]){
			case "+":
				answer += getals[i];
			break;
			case "-":
				answer -= getals[i];
			break;
			case "/":
				answer /= getals[i];
			break;
			case "*":
				answer *= getals[i];
			break;
			case "=":
				answer == getals[i];
			break;
		}
	}
	document.getElementById('theanswer').innerHTML = "Antwoord: " + answer;
}
</script>
<div id="berekening" style="font-size:24px;">Current berekening: No berekening</div>
<form name="mathform" action="" id="mathform">
<label>Add a number:<input type="text" width="100px" name="getalA" id="getalA" /></label><br />
<input type="button" name="erbij" id="erbij" value="+" onclick="erbijberekening('+', this.form);" />
<input type="button" name="eraf" id="eraf" value="-" onclick="erbijberekening('-', this.form);" />
<input type="button" name="delen" id="delen" value="/" onclick="erbijberekening('/', this.form);" />
<input type="button" name="keren" id="keren" value="*" onclick="erbijberekening('*', this.form);" /><br />
<input type="button" name="getal" id="isteken" value="=" onclick="erbijberekening('=', this.form);">
<input type="button" name="solve" value="BEREKEN!!" onclick="findAntwoord();" /><br /><hr />
<input type="reset" name="clear" value="OPNIEUW!!" onclick="resetForm(this.form);" /><hr />
</form>
<div id="theanswer">Antwoord zal here worden weergegeven!!!</div>
</body>
</html>
can you solve this for me too please?/?

i put some dutch terms in it... sorry for that...;)
 
Last edited:

VPmase

New Member
Messages
914
Reaction score
1
Points
0
Hmm... It's going to take me a while to figure the problem out so I made you a simpler one.
http://www.saumpro.com/EsamTests/test3.html

HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Calculator</title>
</head>

<body>
<script type="text/javascript">
var cureq = "";
var getal1;
var getal2;

function erbijberekening(sign, aform){
	if(!isNaN(parseInt(aform.getalA.value)) && aform.getalA.value.length > 0){
		if(!isNaN(parseInt(aform.getalB.value)) && aform.getalB.value.length > 0){
			switch(sign){
				case "+":
					answer = parseInt(aform.getalA.value) + parseInt(aform.getalB.value);
				break;
				case "-":
					answer = parseInt(aform.getalA.value) - parseInt(aform.getalB.value);
				break;
				case "/":
					answer = parseInt(aform.getalA.value) / parseInt(aform.getalB.value);
				break;
				case "*":
					answer = parseInt(aform.getalA.value) * parseInt(aform.getalB.value);
				break;
			}
			document.getElementById('theanswer').innerHTML = "Antwoord: " + answer;
		} else {
			alert("Please enter a real getal for Number 2!");
		}
	} else {
		alert("Please enter a real getal for Number 1!");
	}
}

function resetForm(aform){
	for(i=0;i<countnums;i++){
		getals[countnums] = 0;
		signs[countnums] = "";
	}
	aform.erbij.disabled = false;
	aform.eraf.disabled = false;
	aform.delen.disabled = false;
	aform.keren.disabled = false;
	countnums = 0;
	cureq = "";
	document.getElementById('berekening').innerHTML = "Current berekening: No berekening";
	document.getElementById('theanswer').innerHTML = "Antwoord zal here worden weergegeven!!!";
}

</script>
<div id="berekening" style="font-size:24px;">Current berekening: No berekening</div>
<form name="mathform" action="" id="mathform">
<label>Number 1:<input type="text" width="100px" name="getalA" id="getalA" /></label><br />
<label>Number 2:<input type="text" width="100px" name="getalB" id="getalB" /></label><br />
<input type="button" name="erbij" id="erbij" value="+" onclick="erbijberekening('+', this.form);" />
<input type="button" name="eraf" id="eraf" value="-" onclick="erbijberekening('-', this.form);" />
<input type="button" name="delen" id="delen" value="/" onclick="erbijberekening('/', this.form);" />
<input type="button" name="keren" id="keren" value="*" onclick="erbijberekening('*', this.form);" /><br />
<input type="reset" name="clear" value="OPNIEUW!!" onclick="resetForm(this.form);" /><hr />
</form>
<div id="theanswer">Antwoord zal here worden weergegeven!!!</div>
</body>
</html>
 

qwertzguy

New Member
Messages
4
Reaction score
0
Points
0
Hello,

Here is your original code working perfectly (lots of little errors, and the main one was: bereken() is not a valid function name, so I add an 'i')

<html>
<head><title>TEST</title>
<script type="text/javascript">
/*i'm dutch so maybe there will be some terms you don't get!!*/
function plusje(form) {
document.getElementById("number").disabled=true;
document.getElementById("sign").value='+';
}

function minnetje(form) {
document.getElementById("number").disabled=true;
document.getElementById("sign").value='-';
}

function keertje(form) {
document.getElementById("number").disabled=true;
document.getElementById("sign").value='X';
}

function deeltje(form) {
document.getElementById("number").disabled=true;
document.getElementById("sign").value='/';
}

function resetje(form) {
document.getElementById("number").disabled=false;
document.getElementById("number").value='1';
document.getElementById("sign").value='';
document.getElementById("number2").disabled=false;
document.getElementById("number2").value='1';
}

function bereiken() {
var sign = document.getElementById("sign").value;

if (sign == '+')
{
erbij();
}
else if (sign == '-')
{
eraf()
}
else if (sign == 'X')
{
keren()
}
else if (sign == '/')
{
delen()
}
else
{
alert ("Fill in a sign!!")
}
}

function erbij() {
var number1 = document.getElementById("number").value;
var number2 = document.getElementById("number2").value;
var answer = parseInt(number1) + parseInt(number2);
var result = document.getElementById("result");
result.innerHTML = '';
result.innerHTML += answer;
if (answer > 100000 || answer < -100000)
{
alert ("ERROR: number is too big or too small!!");
}
}

function eraf() {
var number1 = document.getElementById("number").value;
var number2 = document.getElementById("number2").value;
var answer = number1 - number2;
var result = document.getElementById("result");
result.innerHTML = '';
result.innerHTML += answer;
if (answer > 100000 || answer < -100000)
{
alert ("ERROR: number is too big or too small!!");
}
}

function keren() {
var number1 = document.getElementById("number").value;
var number2 = document.getElementById("number2").value;
var answer = number1 * number2;
var result = document.getElementById("result");
result.innerHTML = '';
result.innerHTML += answer;
if (answer > 100000 || answer < -100000)
{
alert ("ERROR: number is too small or too big!!");
}
}

function delen() {
var number1 = document.getElementById("number").value;
var number2 = document.getElementById("number2").value;
var answer = number1 / number2;
var result = document.getElementById("result");
result.innerHTML = '';
result.innerHTML += answer;
if (answer > 100000 || answer < -100000)
{
alert ("ERROR: number is too small or too small!!!!");
}
}
</script>
</head>
<body>
<form name="form" id="form">
<input type="text" value="1" name="number" id="number">
<input type="text" size="1" value="" id="sign" style="border:0;" name="sign" readonly>
<input type="text" value="1" name="number2" id="number2"> = <div id="result" anem="result"></div>
<br>
<hr />
<input type="button" value="+" name="plus" onClick="plusje(this.form)">
<input type="button" value="-" name="min" onClick="minnetje(this.form)">
<input type="button" value="X" name="product" onClick="keertje(this.form)">
<input type="button" value="/" name="divide" onClick="deeltje(this.form)">
<br>
<hr />
<input type="button" value="OPNIEUW" name="opnieuw" id="opnieuw" onClick="resetje(this.form)">
<br>
<hr />
<input type="button" value="bereken" name="bereken" onClick="bereiken()">
</form>
</body>
</html>
 
Top