Wrox Programmer Forums
|
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 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
  Spam!  
Old December 19th, 2012, 06:13 PM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question Chapter5_Exercises_Question#3

I am doing exercise question 3 chapter 5 and I am having problems. We are supposed to modify a function so that it can be displayed with a certain amount of decimal places which are determined by the user. The algorithm that is used in the function that is given as the answer makes no sense to me. Could someone explain to me what is going on in the function below? The program works, I just don’t understand how. I will try to use comments so the person answering my question could better help me.

Thanks,
Truck35

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Chapter 5: Question 3</title>
    <script type="text/javascript">
//function called fix is created to take two parameters

    function fix(fixNumber, decimalPlaces)
    {
/* pow method is used to take 10 and raise it to the power that is given by user (decimalPlaces)*/

        var div = Math.pow(10,decimalPlaces);
/*fixNumber passed by fix function is multiplied by value assigned to div variable.  It is then passed to the round method and then divided by the number  assigned to the div variable.*/
        fixNumber = new String(Math.round(fixNumber * div) / div);

/*I don't understand fully understand what's going on here.  I need someone to explain*/     
  if (fixNumber.lastIndexOf(".")==-1)
        {
//I don't understand fully what's going on here.
            fixNumber = fixNumber + ".";
        }

//I don't understand fully what's going on here.
        var zerosRequired = decimalPlaces - 
            (fixNumber.length - fixNumber.lastIndexOf(".") - 1);

/*I know this is a for loop and that zerosRequired was initialized in the pervious statement but, I don't fully understand what's going on here*/
        for (; zerosRequired > 0; zerosRequired--)
        {
//I don't fully understand what's going on here either.
            fixNumber = fixNumber + "0";
        }
//This is the value that is returned when the method is called
        return fixNumber;
    }
    </script>
</head>
<body>
<script type="text/javascript">
//I understand this code below

var number1 = prompt("Enter the number with decimal places you want to fix","");
var number2 = prompt("How many decimal places do you want?","");

document.write(number1 + " fixed to " + number2 + " decimal places is: ");
document.write(fix(number1,number2));
</script>
</body>
</html>









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