Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To 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
 
Old May 27th, 2004, 08:40 AM
Authorized User
 
Join Date: Sep 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default problems with examples in book

this is the code for exercise 3 chap 4 in beginning javascript. I keep getting an error when I run it and need help solving.

<html>
<head>
<script language=javascript>
function fix(fixNumber, decimalPlaces)
{
var div = Math.pow(10, decimalPlaces);
fixNumber = new String(Math.round(fixNumber * div) / div);

If (fixNumber.lastIndexOf(".") == -1)
{
fixNumber = fixNumber + ".";
}
var zerosRequired = decimalPlaces - (fixNumber.length - fixNumber.lastIndexOf(".") -1);


for (zerosRequired = 0; zerosRequired > 0; zerosRequired--)
{
fixNumber = fixNumber + "0";
}
return fixNumber;
}
</script>
</head>
<body>
<script language=javascript>
var number1 = prompt("enter a number","");
var number2 = prompt("enter number of decimal places","");

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

 
Old May 27th, 2004, 08:45 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi skicrud,

The line:

If (fixNumber.lastIndexOf(".") == -1)

should start with "if" (all lowercase).

HTH,

Chris

 
Old May 27th, 2004, 08:52 AM
Authorized User
 
Join Date: Sep 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks, I wish WROX would be more carefull in their beginner books






Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference in book and examples golf1052 BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 1 June 15th, 2007 04:14 PM
Problems with Examples of Beginning with php4 Book xtimox Beginning PHP 2 December 17th, 2004 02:01 PM
Problems with the examples from the J2EE book alf.guzman J2EE 0 April 20th, 2004 09:43 AM
Book examples duff? Vormulac ASP.NET 1.0 and 1.1 Basics 2 September 8th, 2003 04:37 AM
Book Examples luisfco Pro VB 6 1 September 5th, 2003 06:50 PM





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