Monday, January 31, 2011

WEEK 1 - Track1

I watched the first video of the Douglas Crockford lecture series and also read the first three chapters of Eloquent Javascript. I personally feel one will benefit more if one watches the video after reading the chapters. I also consulted the JavaScript book by David Flannagan(the Rhinocerous book) which Crockford had recommended in the video.
Here are the answers to the questions :

1. The alliance of Netscape and Sun Microsystems did not use Java in the browser… Why do you think Java was not suitable to be embedded in a browser?
Java was simply too heavy a language and not suitable for browser scripting. In the 90's, most of the computers had dial-up connection and so, the load-and-go feature of Javascript was very useful for fast loading of web pages. Also, Java is a much more complex language than Javascript, on the lines of c++, with libraries, etc. So, the people at Netscape felt that they needed a simpler and hassle free language which could make browser scripting easier.

2. When parsing a String into a number using the parseInt() method, we are advised to always provide the radix. Why is it so strongly recommended?
The function parseInt(value, radix) converts the given value to a number according to the base or radix. Javascript converts the value to a number and stops when it encounters the first non-digit character in the given value according to the particular radix.Now suppose we enter the values as "08", "09" or "008" without a radix, JS might interpret the string as an octal integer. Since, only numbers 0-7 are allowed in octal system, it will terminate immediately as it encounters the value 8 or 9 thus giving the result in the three cases as 0. Hence, radix is always recommended.

3. What is a type, and why do you think types are useful in writing programs?
A type determines the form or category of value that a variable can contain. Types are extremely useful in classifying data. JS has the following types: numbers, strings, boolean, objects, undefined and null. Data types make it extremely convenient for us to perform various operations with the data at hand like comparing their values, performing addition, subtraction, etc. Data types prevent us from engaging in faulty operations on data like comparing a string and an integer. However, JS does have a few problems when we work on data of particular type( for example loss of precision in numbers.) Also, the + operator is used to both add numbers as well as concatenate strings. So, '2' + '4' gives 24.

4. Why do we lose precision when performing operations with decimal numbers in Javascript? Can you think of a few implications of why this would be a problem?
Javascript interprets all numbers as 64-bit floating point. 1 bit is used for the (+/-) sign in front of the numbers, 11 bits for the decimal point and remaining 52 bits for the number. For whole numbers that use 52 bits, this is not a problem. However, for pure decimal numbers, we end up losing precision when we perform arithmetic operations on them. For example, 0.1 + 0.2 gives 0.30000000000000004 as the answer as it uses up all 52 bits for providing the answer. Hence, while performing calculations which require precision like currency, we must always multiply the values by 100, and then divide the answer by 100.

5. Do you understand why the following operation produces the given result 115 * 4 – 4 + 88 / 2 = 500?
This is due to the fact that JS follows the standard rule of arithmetic operation: BODMAS( Brackets, orders or exponents, division, multiplication, addition, subtraction). So, for the given expression, first (115*4) and (88/2) is evaluated and the exp. becomes 460-4+44 which is equal to 500.

6. What does typeof 4.5 do, and why does typeof (typeof 4.5) return “string” ?
The typeof operator produces a string value naming the data type of the given value. Thus, typeof 4.5 will give "number"(without the quotes) as the output. typeof(typeof 4.5) is same as typeof("number"). As, "number" is a string, the output is the string "String" ( without the quotes).






No comments:

Post a Comment