Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript 4th Edition
Password Reminder
Register
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 December 20th, 2012, 10:34 AM
Authorized User
Points: 132, Level: 2
Points: 132, Level: 2 Points: 132, Level: 2 Points: 132, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question chapter5_Execercise_Question3

I am doing exercise question 3 chapter 5 and I am having problems. We are supposed to modify a function so that it can display 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 created to take two parameters*/

    function fix(fixNumber, decimalPlaces)
    {
/*pow method is called to raise 10 to the power entered by user (decimalPlaces) then assigned to the varaible div*/

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

/*fixNumber is multiplied by div then passed to the round method were it is divided by div.  It is then assigned to fixNumber*/

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

//I don't fully understand what's happening here.
        if (fixNumber.lastIndexOf(".")==-1)
        {

//I don't full understand what's happening here.
            fixNumber = fixNumber + ".";
        }

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

//I don't full understand what's happening here.
        for (; zerosRequired > 0; zerosRequired--)
        {
//I don't full understand what's happening here.
            fixNumber = fixNumber + "0";
        }
/*The value "fixNumber" is returned when method is called
        return fixNumber;
    }
    </script>
</head>
<body>
<script type="text/javascript">

/*I understand all the 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>
Reply With Quote
  #2 (permalink)  
Old December 20th, 2012, 04:26 PM
Authorized User
Points: 132, Level: 2
Points: 132, Level: 2 Points: 132, Level: 2 Points: 132, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Smile solved it myself

I used a "document.write" code to dislplay every varaible and instance varaible to see what was in them. I then was able to figure out what the person that wrote the code was doing. I had to do it this way because for some reason my debugger stoped working.

The code below is the code I was having problems with. I've added comments to explain what each block of code is doing in the function.

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 called fixNumber and decimalPlaces*/

    function fix(fixNumber, decimalPlaces)
    {
/*pow method takes the value 10 and raises it to the number that is stored in the variable called decimalPlaces*/

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

/*value stored in fixNumber variable is multiplied by value in div variable.  This number is then rounded using the round method and divided by the value in the div variable,  The number is then changed into a string object using the new String() syntax then stored in the variable called fixNumber.*/

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

/*This "if" statement uses the "lastIndexOf" method to see if the period is in the string.  If the value returned is equale to -1(Meaning if the period is not found) a period is added to the string stored in the fixNumber Method*/

        if (fixNumber.lastIndexOf(".")==-1)
        {
            fixNumber = fixNumber + ".";
        }

/*  This takes the number of decimalPlaces given by the user minus the fixNumber's length minus the value returned by the "lastIndexOf" method minus one.  This number is then assigned to the zeroRequired varaible. */

        var zerosRequired = decimalPlaces - 
            (fixNumber.length - fixNumber.lastIndexOf(".") - 1);
            
/*This for loop checks to see if the number in zerosRequired is greater that 0.  If it is, it adds 0's to the fixNumber string varaible and decrements the number until  the zerosRequired value is no longer greater than zero.*/

        for (; zerosRequired > 0; zerosRequired--)
        {
            fixNumber = fixNumber + "0";
        }
/*Returns value when fixNumber function is called.
        return fixNumber;
    }
    </script>
</head>
Bless,
Truck35
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



All times are GMT -4. The time now is 09:53 AM.


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