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 10-17-2012, 04:56 PM   PM User | #1
aslanali555
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
aslanali555 is an unknown quantity at this point
Can you help me at calculator java script?

<!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>
<script type="text/javascript">

//variables
var firstValue=0;
var secondValue=0;
var result=0;

addClick=false;
minusClick=false;

function numberButtonPressed(number){
document.form1.inputValue.value+=number;
}

function addButtonPressed(){
firstValue=parseInt(document.getElementById("inputValue").value);
document.form1.inputValue.value="";
addClick=true;
minusClick=false;
}

function minusButtonPressed()
{
firstValue=parseInt(document.getElementById("inputValue").value);
document.form1.inputValue.value="";
addClick=false;
minusClick=true;
}
function equalButtonPressed(){
secondValue=parseInt(document.getElementById("inputValue").value);
document.form1.inputValue.value="";

if (addClick==true){
result=firstValue+secondValue;
document.getElementById("inputValue").value=result;
}
else if (minusClick==true){
result=firstValue-secondValue;
document.getElementById("inputValue").value=result;
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="" action="">
<input id = "inputValue" type = "text" size = "40" />
<p> <input name="button4" type = "button"
onclick = "numberButtonPressed(7)" value = "7" />
<input name="button5" type = "button"
onclick = "numberButtonPressed(8)" value = "8" />
<input name="button6" type = "button"
onclick = "numberButtonPressed(9)" value = "9" />
<input name="button12" type = "button"
onclick = "addButtonPressed()" value = "+" />
<p>
<input name="button" type = "button"
onclick = "numberButtonPressed(4)" value = "4" />
<input name="button2" type = "button"
onclick = "numberButtonPressed(5)" value = "5" />
<input name="button9" type = "button"
onclick = "numberButtonPressed(6)" value = "6" />
<input name="button13" type = "button"
onclick = "minusButtonPressed()" value = "-" />
<p>
<input name="button3" type = "button"
onclick = "numberButtonPressed(1)" value = "1" />
<input name="button7" type = "button"
onclick = "numberButtonPressed(2)" value = "2" />
<input name="button8" type = "button"
onclick = "numberButtonPressed(3)" value = "3" />
<input name="button10" type = "button"
onclick = "numberButtonPressed(0)" value = "0" />
<input name="button14" type = "button"
onclick = "equalButtonPressed()" value = "=" />

</form>
</body>
</html>




This calculator normally runs.For example; I entered number. Then I clicked "+" or "-", I entered another number and finally I click "=". This is no problem.

But like this, I entered number. Then I clicked "+" and "-" , I entered another number and finally I click "=". Give error. Write "NaN"


I want to be current last clicked..

Please help me..Thank you in advance..
aslanali555 is offline   Reply With Quote
Old 10-17-2012, 06:02 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I think this fixes it!

Code:
function addButtonPressed(){
firstValue=parseInt(document.getElementById("inputValue").value);
if (isNaN(firstValue)) { firstValue = fv2}
fv1 = firstValue;
document.form1.inputValue.value="";
addClick=true;
minusClick=false;
}

function minusButtonPressed() {
firstValue=parseInt(document.getElementById("inputValue").value);
if (isNaN(firstValue)) { firstValue = fv1}
fv2 = firstValue;
document.form1.inputValue.value=""; 
addClick=false; 
minusClick=true;
}

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 10-17-2012, 06:11 PM   PM User | #3
aslanali555
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
aslanali555 is an unknown quantity at this point
Really, thank you very much.
problem is solved..
aslanali555 is offline   Reply With Quote
Old 10-18-2012, 07:01 PM   PM User | #4
aslanali555
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
aslanali555 is an unknown quantity at this point
<!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>
<script type="text/javascript">

//variables
var firstValue=0;
var secondValue=0;
var result=0;

var addClick;
var minusClick;

function numberButtonPressed(number)
{
document.form1.inputValue.value+=number;
}

function buttonPressed(fonkClick)
{
firstValue=parseInt(document.getElementById("inputValue").value);
document.form1.inputValue.value="";
minusClick=false;
addClick=false;

fonkClick=true;
}

function equalButtonPressed()
{
secondValue=parseInt(document.getElementById("inputValue").value);
document.form1.inputValue.value="";
if (addClick==true)
{
result=firstValue+secondValue;
document.getElementById("inputValue").value=result;
}
else if (minusClick==true)
{
result=firstValue-secondValue;
document.getElementById("inputValue").value=result;
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="" action="">
<input id = "inputValue" type = "text" size = "40" />
<p> <input name="button4" type = "button"
onclick = "numberButtonPressed(7)" value = "7" />
<input name="button5" type = "button"
onclick = "numberButtonPressed(8)" value = "8" />
<input name="button6" type = "button"
onclick = "numberButtonPressed(9)" value = "9" />
<input name="button12" type = "button"
onclick = "buttonPressed(addClick)" value = "+" />
<p>
<input name="button" type = "button"
onclick = "numberButtonPressed(4)" value = "4" />
<input name="button2" type = "button"
onclick = "numberButtonPressed(5)" value = "5" />
<input name="button9" type = "button"
onclick = "numberButtonPressed(6)" value = "6" />
<input name="button13" type = "button"
onclick = "buttonPressed(minusClick)" value = "-" />
<p>
<input name="button3" type = "button"
onclick = "numberButtonPressed(1)" value = "1" />
<input name="button7" type = "button"
onclick = "numberButtonPressed(2)" value = "2" />
<input name="button8" type = "button"
onclick = "numberButtonPressed(3)" value = "3" />
<input name="button10" type = "button"
onclick = "numberButtonPressed(0)" value = "0" />
<input name="button14" type = "button"
onclick = "equalButtonPressed()" value = "=" />

</form>
</body>
</html>


This program doesn't calculate the result.
Can you tell me problem here.
Thankyou in advance
aslanali555 is offline   Reply With Quote
Old 10-18-2012, 07:55 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I thought you said that your problem was solved?

For simple typos etc. use your error console. What is fonkclick supposed to be?

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

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 03:04 AM.


Advertisement
Log in to turn off these ads.