p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 (http://p2p.wrox.com/book-beginning-javascript-3rd-ed-isbn-978-0-470-05151-1-331/)
-   -   PROBLEM WITH CHAPTER 4, EXERCISE 3 (http://p2p.wrox.com/book-beginning-javascript-3rd-ed-isbn-978-0-470-05151-1/82419-problem-chapter-4-exercise-3-a.html)

rhieger January 28th, 2011 03:34 PM

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 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) );


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.


Robert Hieger

All times are GMT -4. The time now is 11:18 AM.

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