Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 3rd, 2014, 07:34 PM
Registered User
 
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>
 
Old August 3rd, 2014, 08:17 PM
Registered User
 
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?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 18 Different results econophil BOOK: JavaScript 24-Hour Trainer 1 February 5th, 2013 10:30 AM
Switch Help - Lesson 18 MinusZero BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 5 December 19th, 2012 08:47 PM
Lesson 18 Exercise 3 SamW BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 6 December 11th, 2012 12:55 AM
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 02:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.