Appendix A. Answers
Chapter 2
The following are valid:
$someVariable _someVariable some_variable somèvariable
The
functionvariable uses a reserved JavaScript keyword,someVariableand1Variableboth start with invalid characters, andsome*variableuses an invalid character, a JavaScript operator (*), as part of the variable name. All invalid names generate JavaScript errors.The identifiers are converted as follows:
The variable
some-monthbecomessomeMonth, using CamelCase notation.The function
theMonthbecomesgetTheCurrentMonth, using relevant verbs and other distinguishing information.The const
current-monthbecomesCURRENT-MONTH, using constant-width uppercase letters.The variable
summer-monthbecomessummerMonths, maintaining consistency between the array of items and variable name.The
MyLibrary-afunctionfunction becomesmylibraryFunctionverbFunctionname.Important point to remember: conjugation is the bane of coders. Use a backslash before the inner single quote so that itâs interpreted literally, and not end-of-string:
var someString = âWho once said, âOnly two things are infinite, the universe and human stupidity, and I\âm not sure about the former.ââ;
The following code would work:
var fltNumber = 432.54; var intNumber = parseInt(fltNumber); var octNumber = parseInt(intNumber,2); var hexNumber = parseInt(intNumber,16);
The function
parseIntreturns the decimal base integer of the floating-point number, which is 432. It can also take a second parameter, specifying ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access