Monday, January 31, 2011

Javascript:101, Week 1, Reflections

After a really hectic end to last week it's finally time for me to start working on my two P2PU courses.

This post addresses the questions to reflect on for week 1.

Questions for reflection:
  1. The alliance of Netscape and Sun Microsystems did not use Java in the browser, because it was not suitable for that purpose. Why do you think Java was not suitable to be embedded in a browser?

    Java was far too robust. If they embedded Java in a browser it would have been too bulky and the websites would have been very difficult to make and very clumsy and slow. Would probably not have been fully 'load and go'
  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?

    Giving parseInt() a radix argument tells the function in which base to parse the number. If you don't specify this it could interpret the number incorrectly. For example, when parsing '08' it may see the leading zero and decide that it is in octal. It will then see the 8 and terminate, giving you the result '0'.
  3. What is a type, and why do you think types are useful in writing programs?

    The type of an object lets the compiler know how much space the object might take up. Boolean values, for example, can only have 2 possible values, while numbers can have 2^64 values. If a language had no types then the compiler would most likely be made to assume that all objects needed the maximum amount of space needed for all types, meaning that it would set aside the same space for a binary value like a bool as it would for a more complex object like an array or hash table of strings. This would be very inefficient, and probably easy to exploit as well.
  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?

    JS doesnt handle arithmetic well with decimals. The common example is 0.1 + 0.2 doesn't come out to 0.3. It returns something like 0.30000000000000004. So the way around this is to take numbers and multiply them by 100 or whatever so they become whole numbers, do the math, then divide by 100 and display the results to the appropriate precision. This is a work around but with both you can end up with discrepancies over time. This can be dangerous when dealing with money.
  5. Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500

    Order of operations. First (), then * /, then +-. So the above works as:
    115 * 4 - 4 + 88 / 2
    460 - 4 + 44
    500
  6. What does typeof 4.5 do, and why does typeof (typeof 4.5) return "string" ?

    The typeof() function returns a string telling you the type of the given value. (note: there are some errors.) The string "number" is returned for typeof(4.5) and typeof(typeof(4.5)) returns string because the inner call to typeof returns a string value.

No comments:

Post a Comment