Errata

JavaScript Cookbook

Errata for JavaScript Cookbook

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted By Date submitted Date corrected
PDF
Page darcyparker@gmail.com
darcyparker@gmail.com

In Table 1-1: First example, there is a line:

if (sVal == "this) // true

Should be:

if (sVal == "this") // true

Note from the Author or Editor:
In table 1-1, first row, the second line of the example should read

if (sVal == "this") // true

Darcy Parker  Mar 04, 2011 
PDF
Page darcyparker@gmail.com
darcyparker@gmail.com

Chapter 6, Section 6.8:
This is actually an example of Partial Evaluation/Partial Application and not Currying. There's a subtle difference.

See articles like: http://msdn.microsoft.com/en-us/scriptjunkie/gg575560.

Wikipedia also has a note about this confusion: http://en.wikipedia.org/wiki/Currying See: "Contrast with partial function application"

Note from the Author or Editor:
Note: do not make a change to new printings, this is an advisory only.

The errata submitter makes a good point: is this section an example of currying? Or partial evaluation? Addressing the change is too complex to be handled in traditional errata processing. However, I will re-visit the section in new editions, and want to ensure readers are presented with this additional information.

Anonymous  Mar 04, 2011 
Other Digital Version
1.4
1.4 Finding a Substring in a String

From the Android Marketplace app.

var txtString = "This apple is my apple";
var iValue = tstString.lastIndexOf("apple"); // returns 17,
// index of last occurrence of substring

// my comment
// tstString should be txtString, or the other way around.

Note from the Author or Editor:
In section 1.5.3, change

var txtString = "This apple is my apple";

to

var tstString = "This apple is my apple";

Anonymous  Apr 30, 2011 
Other Digital Version
1.4
1.4 Finding a Substring in a String

Hi Shelley,
(From the Android Market App book

I'm still new to JavaScript, and I'm confused by the section on lastIndexOf - especially when passing an argument to this method.

The book says that the argument is an index value of where to start the search, counted from the right. I can't understand the examples if that's the case.

The Mozilla pages at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/lastIndexOf say that the argument is "The location within the calling string to start the search from, indexed from left to right. It can be any integer between 0 and the length of the string. The default value is the length of the string."

The book also says; "Notice that the value returned from lastIndexOf changes based on the starting position, as counted from the string?s right." - but that doesn't seem to be the case using the examples -

ie -

var val1 = "This apple is my apple".lastIndexOf("apple",12);
console.log(val1); // returns 5

and

var val2 = "This apple is my apple".lastIndexOf("apple",11);
console.log(val1); // also returns 5

I'm enjoying this book a lot so far - but I'm really confused by this.

Thanks




Note from the Author or Editor:
Change the following line

Like indexOf, lastIndexOf also takes an optional second parameter, which is an index value of where to start the search, counted from the right:

To

Like indexOf, lastIndexOf also takes an optional second parameter, which is an index value of where to start the search, counted from the left:

Anonymous  Apr 30, 2011 
PDF
Page 7
Table 1-1. Comparison operators

var sOne = "Cat";
var sTwo = "cat";
if (sOne >= sTwo) // true
should be false because ASCII values are compared ("C" < "c")

Note from the Author or Editor:
For Table 1-1 change example:

var sOne = "Cat";
var sTwo = "cat";

if (sOne >= sTwo) // true

to

var sOne = "Cat";
var sTwo = "cat";

if (sOne >= sTwo) // false

Change example:

var sOne = "cat";
var sTwo = "Cat";

if (sOne < sTwo) // true

To

var sOne = "cat";
var sTwo = "Cat";

if (sOne < sTwo) // false

Change example and preceding sentence within text from:

If two string literals only vary based on case, the uppercase characters are lexically greater than the lowercase letter:

var sOne = "Cat";
var sTwo = "cat";
if (sOne >= sTwo) // true, because 'C' is lexically greater than 'c'

to:

If two string literals only vary based on case, the uppercase characters are less than the lowercase letter:

var sOne = "Cat";
var sTwo = "cat";
if (sOne >= sTwo) // false, because 'c' is greater than 'C'

robert_  Jul 22, 2010 
Printed
Page 7
First code example after Table 1-1

Missing closing parenthesis:

if (sOne > sTwo // false, because "cat"?

Should be:

if (sOne > sTwo) // false, because "cat"?

Note from the Author or Editor:
Alter text in first code block following Table 1-1 from

if (sOne > sTwo

To

if (sOne > sTwo)

Callum Macrae
 
Feb 25, 2012 
PDF
Page 21
4th paragraph

The text states:
Another consideration is how can we be sure that the values are in octal format? Rather than:
rgb (255, 0, 0)
we might find:
rgb (100%, 0, 0)
---------------------------
The rgb (255, 0, 0) is in decimal not octal values as the text states.

Note from the Author or Editor:
Correct the text to read decimal, not octal.

arcadiabill  Aug 07, 2010 
Printed
Page 21
First paragraph

The line of code that reads:

var iValue = testValue(subsValue);

should be changed to the following:

var iValue = testValue.indexOf(subsValue);

Note from the Author or Editor:
Thanks!

Being corrected in new edition.

Curtis Blaine  Jul 19, 2013 
PDF
Page 28
3rd Para -- Code under Solution paragraph

The text states that:
Use the RegExp exec method and the global flag (g) in a loop to locate all instances of a pattern, such as any word that begins with t and ends with e, with any number of characters in between:

use the regular expression: var pattern = /t\w*e/g;

However, that regex will find words such as "ate" which doesn't begin with a 't' as well as words such as "test" which do not end in an 'e'. In fact, it will find the starting position of the 't' in any word that has a 't' followed by an 'e' anywhere within the word. A better regex expression would be:
/\bt\w*e\b/g

indicating that both the 't' and the 'e' must be on a word boundary.

Note from the Author or Editor:
Change the solution sentence to read:

Use the RegExp exec method and the global flag (g) in a loop to locate all instances of a pattern, such as any word or other text that begins with t and ends with e, with any number of characters in between.

arcadiabill  Aug 07, 2010 
PDF
Page 39
6th line from bottom

Text states that:
getDate Returns day of the month (0?31)

The correct range is (1-31).

Note from the Author or Editor:
In table 3-1 change first entry to:

Returns day of the month (1-31)

arcadiabill  Aug 07, 2010 
PDF
Page 40
5th line from top of page

Text states that:
getUTCDate Returns day of month in UTC time (0?31) method (Date)

Correct range is (1-31).

Note from the Author or Editor:
Change Table 3-1 entry 11 (getUTCDate) to

Returns date of month in UTC time (1-31) method (Date)

arcadiabill  Aug 07, 2010 
60
Solution (4.3) + Solution (4.4) + Discussion (4.4)

random() returns a Number value with positive sign, greater than or equal to 0 but less than 1.

So, in Solution (4.3) the value 255 is not valid. Use 256 in the text and in the code.

Also, recipe 4.4 has the same problem. One way to fix it quickly could be changing the randomVal function:

function randomVal(val) {
return Math. floor(Math.random() * (val + 1));
}

Note from the Author or Editor:
Add to end of discussion for Recipe 4.4:

The Math.random function generates a value between 0 (inclusive) and 1 (exclusive). Multiplying the value by 255, the solution generates a random number in a range between 0 (inclusive), and 255 (exclusive). To allow for a value of 255, multiple the Math.random function result by 256. To ensure the value is never zero (0), add 1 to the Math.floor result.

Modify Recipe 4.5:

Change all values of 255 to 256 in both solution and discussion.

Miguel Mac?as  Jul 15, 2010 
Other Digital Version
70
First code example on page 70

Epub version

var div = document.getElementById("item");
item.innerHTML="<p>" + strValue + "</p>";

Should be:

var div = document.getElementById("item");
item.innerHTML="<p class="rightformatted">" + strValue + "</p>";

The previous paragraph states that a CSS class can be added to the HTML element.

Note from the Author or Editor:
Change code snippet in page 70 to:

var div = document.getElementById("item");
item. innerHTML="<p class='rightformatted'>" + strValue + "</p>";

Anonymous  Sep 19, 2010 
Other Digital Version
75
Second row of table third column

(EPUB Version)


$

Matches end of input

/end?/ matches ?This is the end?

Should be:

$

Matches end of input

/end$/ matches ?This is the end?

Note from the Author or Editor:
Change the third cell in Table 2.1 to:

/end$/ matches "This is the end"

Anonymous  Sep 19, 2010