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 3rd Ed. ISBN: 978-0-470-05151-1
This is the forum to discuss the Wrox book Beginning JavaScript, 3rd Edition by Paul Wilton, Jeremy McPeak; ISBN: 9780470051511
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 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 January 28th, 2011, 04:34 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
Question PROBLEM WITH CHAPTER 4, EXERCISE 3

I am having a problem getting Exercise 3 from chapter 4 of the book to work. At first, I tried to solve the problem on my own and could not. Finally, I resorted to the code in Appendix A and studied it very carefully to see if I understood it. I am still struggling to grasp its logic, but basically understand it.

I then entered the code from Appendix A, p. 665. With very minor deviation from what appears in the text, here is my code:


CODE FOR fix(fixNumber, decimalPlaces) FUNCTION
=====================================

<script type="text/javascript">

/* fix() Function
================================================== ===================

This function takes input of a number and requested number of decimal
places from a user in two separate prompt boxes.

It then calculates what and how many decimal places to display on
the number entered by the user.

================================================== ====================
*/

function fix(fixNumber, decimalPlaces) {

// Declare and initialize variable to hold the exponential value of
// decimal places for the number entered by the user.

var div = Math.pow(fixNumber, decimalPlaces);

// Declare String object used for searching position of decimal place.

fixNumber = new String(Math.round(fixNumber * div) / div);

// Search new String object for position of decimal place.

if (fixNumber.lastIndexOf(".") == -1) {

fixNumber = fixNumber + "."; // If no decimal places, add only
// decimal point to string.

} // end if

// Determine zeroes required.

var zeroesRequired = decimalPlaces -
( fixNumber.length - fixNumber.lastIndexOf(".") - 1 )

for (; zeroesRequired > 0; zeroesRequired--) {

fixNumber = fixNumber + "0";

} // end for

return fixNumber; // Returns value of fixNumber to calling function.

} // end fix(fixNumber, decimalPlaces)

</script>

CODE FOR SCRIPT WITHIN HTML BODY
============================

<script type="text/javascript">

// Prompt for user to enter a number.

var number1 = prompt("Enter a number with decimal places whose number of decimal places\nyou wish to fix: ", "");
var number2 = prompt("How many decimal places do you want?", "");

// Now print value to browser window.

document.write(number1 + " fixed to " + number2 + " decimal places is: ");
document.write( fix(number1, number2) );

</script>

A sample run of the web page where number1 = 2.10246 and number2 = 3 produced the following output in every browser I tested:

2.10246 fixed to 3 decimal places is: 2.1520223122955024


The expected result, according to the book, is 2.100. No matter what I do so far, I cannot produce this result.

Any insight or suggestions would be greatly appreciated.

Sincerely,

Robert Hieger
Reply With Quote
  #2 (permalink)  
Old October 11th, 2014, 04:45 PM
Registered User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Try this

Try to change this code
fixNumber = new String(Math.round(fixNumber * div) / div);
into this
fixNumber = (Math.round(fixNumber * div) / div);

String cannot be multiply by number, you create fixNumber as a new instance of String, you are traying to dedicate fixNumber as string.
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 9 exercise problem 1 fortran_ii BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 2 December 21st, 2011 05:38 AM
problem in the exercise chapter 6 derrida BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 March 16th, 2007 09:48 AM
Chapter 2 Last Exercise has some problem james_027 BOOK: Beginning JavaServer Pages 1 May 25th, 2005 09:19 PM
Problem with Chapter 2, Exercise 2 paultf BOOK: Beginning Java 2 3 December 1st, 2003 06:13 AM
Problem with exercise in chapter 19 backslash BOOK: Beginning Visual C++ 6 4 September 5th, 2003 12:46 PM



All times are GMT -4. The time now is 12:07 AM.


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