Tuesday, 31 May 2011

Javascript week 3 questions for reflection

1.      What is encapsulation, and what do functions encapsulate?

Encapsulation means variables defined in a scope are not visible outside the scope.
Function scopes can create encapsulate, as variables declared in functions are not visible outside of the function. You can use anonymous function to wrap your application.

2.      What is a pure function?

The defining properties of pure functions are that they always return the same value when given the same arguments, and never have side effects. They take some arguments, return a value based on these arguments, and do not monkey around with anything else.

3.      A recursive function must have some sort of an end condition. Why would we get a "out of stack space" error message if a recursive function does not have an end condition?

Recursive function calls same function until the end condition is met. When a function is called, memory space is allocated for the local variables within the function and they are pushed in the stack. When function exit, the space on the stack is removed.
If recursive function does not have end condition, stack will go on adding local variable and we will reach out of space on stack.

4.      Reflect about the difference between object inheritance and class inheritance

Class Inheritance:
·         There are classes and instances. Both are two different entities
·         Classes are defined and initiated with constructor method.
·         Construct an object hierarchy by using class definitions to define subclasses of existing classes.
·         Inherit properties by following the class chain.
·         Class definition specifies all properties of all instances of a class. No way to add properties dynamically at runtime.

Object Inheritance:
·         Every object is instance.
·         Set of objects are defined and created with constructor functions.
·         Construct an object hierarchy by assigning an object as the prototype associated with a constructor function.
·         Inherit properties by following the prototype chain.
·         Constructor function or prototype specifies an initial set of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects. 

5.      What is object augmentation, and how do we do it?

Object augmentation is a process, in which new members can be added to any object by simple assignment. There is no need to define new class, as there are no classes in JavaScript.
Suppose there is an object  :
 var vObj = {
Id : 1,
 State: “Maharashtra”
} ;
                I can add another object or property to this object at same time or later on:
var vObj = {
Id : 1,
 State: “Maharashtra”,
City : {  CityId : 12,
Title: “Pune”}
} ;


Javascript week 2 questions for reflection


1.     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?


Syntax: parseInt(string, radix)
parseInt() is used to convert string to number, but it stops at first non-digit character.
E.g.:      parseInt(“08”) will result in 0
              parseInt (“08”, 10) will result in 8
Radix is optional, it represents number (from 2 to 36) which is a numeral system (16 for hexadecimal, 8 for octal, 10 for decimal...) to be used.

2.     What is a type, and why do you think types are useful in writing programs?

      The type of values a program can work on is called as Data Types. String, Number, Boolean, Null are data types in JavaScript.
Data Types are useful as it manipulates data and produces some kind of results.

3.     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?


            There is only one Number type in JavaScript which is 64 bit floating point (double).

As by nature, floating point has maximum precision, they does not map well to common understanding of arithmetic.
E.g. 0.1 + 0.2 should ideally evaluate to 0.3
But 0.1 + 0.3 actually results to = 0.30000000000000004  

4.     Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500


            Yes. This is because * and / has higher precedence over + and -.
115*4-4+88/2   = (115*4)-4 + (88/2)
                                =460-4+44
                                =456+44
                                =500            

5.     Will the following statement evaluate to true or false (("red" != "car") || (3 >= 9)) && !(((10 * 2) == 100) && true)


            It will evaluate to true.
((“red”! =”car”) || (3>=9)) &&! (((10*2) ==100) &&true)
(True || False) &&! ((20 ==100) && True)
True &&! (False &&true)
True &&! False
True && True
True

6.     What does typeof 4.5 do, and why does typeof (typeof 4.5) return "string" ?


            typeof returns a string that identifies the data type of an expression.
typeof(4.5) returns a string which is “number”
so typeof(typeof(4.5)) returns string

Friday, 20 May 2011

Introduction To Javascript Week 1

Here's a simple example of Javascript to show alert.

<html>
<body>

<h1>My First Web Page</h1>

<script type="text/javascript">
alert("Hello World!")
</script>

</body>
</html>