Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: JavaScript 24-Hour Trainer
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: JavaScript 24-Hour Trainer
This is the forum to discuss the Wrox book JavaScript 24-Hour Trainer Jeremy McPeak; ISBN: 978-0-470-64783-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: JavaScript 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 3rd, 2014, 07:34 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 18, Calculator Issue

I am having a huge issue. I completely lesson 17 with my calculator up and running without any problems, however upon completing lesson 18, my calculator adds strings instead of performing the needed functions.

Is there anything wrong with my code? I've revised it so many times already.
Code:
<html>
<head>
	<title>Lesson 18</title>
	<style type="text/css">
	td {
		border: 1px solid gray; 
		width: 50px;
	} 
	
	#results {
		height: 20px;
	}
	</style>
</head>


<body> 
	<table border="0" cellpadding="2" cellspacing="2">
		<tr>
			<td colspan="4" id="results"></td>
		</tr>
			<td><a href="#" >1</a></td>
			<td><a href="#" >2</a></td>
			<td><a href="#" >3</a></td>
			<td><a href="#" >+</a></td>
		</tr>
		
		<tr>
			<td><a href="#" >4</a></td>
			<td><a href="#" >5</a></td>
			<td><a href="#" >6</a></td>
			<td><a href="#" >-</a></td>
		</tr>
		
		<tr>
			<td><a href="#" >7</a></td>
			<td><a href="#" >8</a></td>
			<td><a href="#" >9</a></td>
			<td><a href="#" >*</a></td>
		</tr>
		
		<tr>
			<td><a href="#"> Clear </a> </td>
			<td><a href="#">0 </a> </td>
			<td><a href="#">= </a> </td>
			<td><a href="#"> /</a> </td>
		</tr>
	</table>
	<script type="text/javascript">
		function addDigit(digit) {
			var resultField = document.getElementById("results");
			resultField.innerHTML += digit;
			
			return false;
		
		} 
		
		function calculate() {
			var resultField = document.getElementById("results");
			resultField.innerHTML = eval(resultField.innerHTML); 
			
			return false;

		}
		
		function reset () {
			var resultField = document.getElementById("results");
			resultField.innerHTML = "";
			
			return false;
		}
		
		function getHandlerFunction(innerHTML) {
			return function() {
			addDigit (innerHTML);
			
			return false;
			};
		}
		
		onload = function() {
			var links = document.getElementsByTagName("a"); 
			var length = links.length; 
		
			for (var i = 0; i < length; i++) {
				var link = links[i];
				var innerHTML = link.innerHTML;
				
				switch (innerHTML) {
				case "Clear": 
					link.onclick = reset;
				break; 
				
				case "=":
					link.onclick = calculate; 
				break; 
				
				default:
				link.onclick = getHandlerFunction(innerHTML);
				break;
			}
		}
	};
	
	
	</script>
</body>
</html>
Reply With Quote
  #2 (permalink)  
Old August 3rd, 2014, 08:17 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found out the problem. To my surprise, I realized that I left spaces for "/" "clear" "0" and "=" within the last table row, and so the Switch function would not recognize the cases . . . This programming business is severely unforgiving isn't it?

Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 18 Different results econophil BOOK: JavaScript 24-Hour Trainer 1 February 5th, 2013 09:30 AM
Switch Help - Lesson 18 MinusZero BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 5 December 19th, 2012 07:47 PM
Lesson 18 Exercise 3 SamW BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 6 December 10th, 2012 11:55 PM
Lesson 18 - TryIt sendalot BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 June 6th, 2012 10:30 AM
Lesson 10 Calculator Applet dthoma128 BOOK: Java Programming 24-Hour Trainer by Yakov Fain 2 December 29th, 2011 01:09 PM



All times are GMT -4. The time now is 04:01 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.