Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning JavaScript 4th Edition
This is the forum to discuss the Wrox book Beginning JavaScript, 4th Edition by Paul Wilton, Jeremy McPeak; ISBN: 978-0-470-52593-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 4th Edition 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 February 12th, 2014, 12:50 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2008
Location: New York, NY, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rhieger
Default Chapter 3, Exercise 4

I have been truly struggling with Exercise 4. I am what you might call an intermediate beginner and if this question has come up before, I apologize, but I searched the forum and could not find a reference to this.

Exercise 4 calls for an automated times table that asks the user for what times table he or she wishes, then calculates and displays it. The process continues until the user enters -1, which breaks out of a while loop.

Being a little stumped, I checked the answer in Appendix A and entered this code. No matter what I did, it did not work properly, so I attempted modification of this code.

What happens no matter what I do is that if a user puts in a value other than -1, the same prompt for a number reappears until -1 is entered. Then the times table is calculated and displayed. In other words, there is an unwanted repetition of the first prompt box. Here is my modified code:

Code:
// Function to output multiplication table.
			
function doMultiplicationTable(tableValue, startValue, endValue) {
				
for (; startValue <= endValue; startValue++) {
					
document.write(tableValue + " * " + startValue + " = " +
(tableValue * startValue) + "<br />");
					
}	// end for
				
}	// end doMultiplicationTable(tableValue, startValue, endValue)
			
/*
*
* Get user input for calculation of timestable. Enclose within a
* while loop whose condition is to continue unless the times table
* value is equal to -1.
*
*/
			
var timesTable;
			
while ( ( timesTable = prompt("What times table do you want calculated?", -1) ) != -1 ) {
				
while ( isNaN(timesTable) ) {
					
timesTable = prompt(timesTable + " is not a valid number. Please retry.", -1);
					
}
				
if (timesTable == -1) {
					
break;
					
}
				
document.write("<p>The " + timesTable + " times table:</p>");
				
doMultiplicationTable(timesTable, 1, 12);
				
}
Any assistance or insight is greatly appreciated.

Sincerely,

Robert Hieger

Last edited by rhieger; February 16th, 2014 at 10:40 PM.
Reply With Quote
  #2 (permalink)  
Old May 15th, 2015, 08:16 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not seeing the same issue. If I make an HTML file and put your code in a script block and run it, I get a prompt, enter say 2, the multiplication table is displayed on the page, and the prompt is then shown again. Repeats as it should until -1 is entered. I even put in a counter variable that I incremented within the outer while loop, and it's only incremented by 1

Code:
var counter = 0;
while ( ( timesTable = prompt("What times table do you want calculated?\nEnter -1 to quit.\nCounter: "+counter, -1) ) != -1 ) {
  .
  .
  .
  counter++
  document.write("<p>The " + timesTable + " times table:</p>");

  doMultiplicationTable(timesTable, 1, 12);

}
Reply With Quote
  #3 (permalink)  
Old September 15th, 2016, 11:39 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why do we use ; (semicolon) at starting of "for" loop

Hello,

I don't have any programming background, I just started learning javascript and got struck at chapter 3 Exercise 3 and 4 of 4th edition:

Why do we use semicolon (;) at starting of FOR loop. It's throwing up error if i don't use that
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
Chapter 3: exercise 2. tobbeebbot BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 3 September 6th, 2012 11:09 PM
Chapter 3 exercise 6 earlmw BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 1 February 10th, 2012 08:10 PM
Chapter 7 - Exercise rickymartini76@yahoo.it BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 2 March 16th, 2010 07:32 PM
Chapter 8 exercise 1 Will BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 March 2nd, 2010 02:26 PM
Chapter 5 exercise 3 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 27th, 2009 02:41 PM



All times are GMT -4. The time now is 10:23 PM.


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