Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
New to JavaScript - need some expert help!
Old 02-19-2009, 07:06 AM New to JavaScript - need some expert help!
Novice Talker

Posts: 6
Name: Martin
Location: Bromsgrove, England UK
Trades: 0
Hey everyone,

I've recently moved into a web development role and so I'm aiming to get as many coding languages learnt as possible. I've started learning JavaScript today, and have coded a JavaScript calculator. I initially had it working, then decided to convert the IF statment for determining the mathmatical symbol to a switch case statement instead. Unfortunately this has stopped it working and I am unsure as to why. I have posted my code below and would appreciate any and all comments as I'm looking to improve my JavaScripting skills

Code:
 
<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>JS Calculator</title>
 <script language="JavaScript">
 
/* This Function places the value of the number button into the display box */
 
 function btnNumber(whichNumber)
 {
 Result.value=(Result.value + whichNumber);
 }
/* This function tells the script which calculation symbol has been chosen and resets the number display box */
 function btnMethod(whichMethod)
 {
 whichfirstValue = Result.value;
 methodValue = (whichMethod);
 Result.value = ("");
 }
/* This function carries out the sum based on  an If deciding how to calculate based on which calculation button was pressed */
 
 function btnResult()
 
 {
 whichValue = Result.value;
 switch(methodValue)
 {
  case "1":
  {whichResult = (parseInt(whichfirstValue) + parseInt(whichValue));
  alert(whichResult);}
  break;
 
  case "2":
  {whichResult = (whichfirstValue - whichValue);
  alert(whichResult);}
  break;
 
  case "3":
  {whichResult = (whichfirstValue * whichValue);
  alert(whichResult);}
  break;
 
  case "4":
  {whichResult = (whichfirstValue / whichValue);
  alert(whichResult);}
  break;
 
  default:
  alert("You must enter some numbers!";
 }
 
 
 }
 
/* Reset button */
 function btnClear()
 {
 Result.value = ("");
 }
 </script>
 
</head>
<body>
<textarea rows="1" cols="6" name="Result">
</textarea>
<br>
<br>
<button type="button" OnClick="btnMethod('1')">+</button>
<button type="button" OnClick="btnMethod('2')">-</button>
<button type="button" OnClick="btnMethod('3')">x</button>
<button type="button" OnClick="btnMethod('4')">/</button>
 
<button type="button" OnClick="btnResult()">=</button>
<br>
<br>
<button type="button" OnClick="btnNumber('1')">1</button>
<button type="button" OnClick="btnNumber('2')">2</button>
<button type="button" OnClick="btnNumber('3')">3</button>
<br>
<button type="button" OnClick="btnNumber('4')">4</button>
<button type="button" OnClick="btnNumber('5')">5</button>
<button type="button" OnClick="btnNumber('6')">6</button>
<br>
<button type="button" OnClick="btnNumber('7')">7</button>
<button type="button" OnClick="btnNumber('8')">8</button>
<button type="button" OnClick="btnNumber('9')">9</button>
<button type="button" OnClick="btnNumber('0')">0</button>
<br>
<br>
<button type="button" OnClick="btnClear()">CLEAR</button>
</body>
</html>

Last edited by zxnet2k; 02-19-2009 at 07:07 AM..
zxnet2k is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-19-2009, 01:19 PM Re: New to JavaScript - need some expert help!
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
You don't have to wrap expression in a switch statement in curly brackets.

So remove the { and } from
case "4":
{whichResult = (whichfirstValue / whichValue);
alert(whichResult);}
break;

==>

case "4":
whichResult = (whichfirstValue / whichValue);
alert(whichResult);
break;
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 02-20-2009, 05:13 AM Re: New to JavaScript - need some expert help!
Novice Talker

Posts: 6
Name: Martin
Location: Bromsgrove, England UK
Trades: 0
hi, thanks for the quick response

I've removed the brackets, but it's still generating an error:

"Object expected on line 95"
zxnet2k is offline
Reply With Quote
View Public Profile
 
Old 02-20-2009, 08:43 AM Re: New to JavaScript - need some expert help!
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I've been experimenting a little with your code, and I found that the following works:
Code:
 
<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>JS Calculator</title>
 <script language="JavaScript">
 
/* This Function places the value of the number button into the display box */
 
 function btnNumber(whichNumber)
 {
 document.getElementById('Result').value=(document.getElementById('Result').value + whichNumber);
 }
/* This function tells the script which calculation symbol has been chosen and resets the number display box */
 function btnMethod(whichMethod)
 {
 whichfirstValue = document.getElementById('Result').value;
 methodValue = (whichMethod);
 document.getElementById('Result').value = ("");
 }
/* This function carries out the sum based on  an If deciding how to calculate based on which calculation button was pressed */
 
 function btnResult()
 
 {
 whichValue = document.getElementById('Result').value;
 switch(methodValue)
 {
  case "1":
  {whichResult = (parseInt(whichfirstValue) + parseInt(whichValue));
  alert(whichResult);}
  break;
 
  case "2":
  {whichResult = (whichfirstValue - whichValue);
  alert(whichResult);}
  break;
 
  case "3":
  {whichResult = (whichfirstValue * whichValue);
  alert(whichResult);}
  break;
 
  case "4":
  {whichResult = (whichfirstValue / whichValue);
  alert(whichResult);}
  break;
 
  default:
  alert("You must enter some numbers!");
 }
 
 
 }
 
/* Reset button */
 function btnClear()
 {
 document.getElementById('Result').value = ("");
 }
 </script>
 
</head>
<body>
<textarea rows="1" cols="6" id="Result">
</textarea>
<br>
<br>
<button type="button" OnClick="btnMethod('1')">+</button>
<button type="button" OnClick="btnMethod('2')">-</button>
<button type="button" OnClick="btnMethod('3')">x</button>
<button type="button" OnClick="btnMethod('4')">/</button>
 
<button type="button" OnClick="btnResult()">=</button>
<br>
<br>
<button type="button" OnClick="btnNumber('1')">1</button>
<button type="button" OnClick="btnNumber('2')">2</button>
<button type="button" OnClick="btnNumber('3')">3</button>
<br>
<button type="button" OnClick="btnNumber('4')">4</button>
<button type="button" OnClick="btnNumber('5')">5</button>
<button type="button" OnClick="btnNumber('6')">6</button>
<br>
<button type="button" OnClick="btnNumber('7')">7</button>
<button type="button" OnClick="btnNumber('8')">8</button>
<button type="button" OnClick="btnNumber('9')">9</button>
<button type="button" OnClick="btnNumber('0')">0</button>
<br>
<br>
<button type="button" OnClick="btnClear()">CLEAR</button>
</body>
</html>
Note: As you might notice, I've replaced Result.value with document.getElementById('Result').value and the textarea has now id="Result" instead of name="Result".
I did this because otherwise it will only work in Internet Explorer.
document.getElementById(''); is a much more supported function.

Note 2: Apparently, you don't have to remove the curly brackets. I didn't really expect that.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 02-23-2009, 04:35 AM Re: New to JavaScript - need some expert help!
Novice Talker

Posts: 6
Name: Martin
Location: Bromsgrove, England UK
Trades: 0
ah brilliant, that works perfectly! I also noticed that I'd missed out a bracket as well which probably wasn't helping

Thanks very much
zxnet2k is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to New to JavaScript - need some expert help!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.71216 seconds with 12 queries