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