Wednesday 15 June 2011

Week-3 First Activity

1.What is encapsulation, and what do functions encapsulate?
   Encapsulation is hide the data and methods in single unit from outside world. To access the data and    methods we need to create instance of that class and access that data through public data members which are declared in that class.

2.What is a pure function?
The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
Examples:
         sin(x), returning the sine of a number x
         length(s), returning the size of a string s

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?
A Recursive function is function which call itself during execution.This enables the function to repeat itself several times, outputting the result and the end of each iteration. In every function call the local variables, result of that each call is saved temprary in Stack and when get the end condition that saved values is reuse for that next procedure of that function. But, when end condition will not met the stack data could not be pop, else local veriable will push continuously in stack and because of no way to emapty the stack when stack will get full the error message will ocur.

4.What is object augmentation, and how do we do it?
In JavaScript, you can add methods to individual objects without the need for additional classes. This has enormous power because you can write far fewer classes and the classes you do write can be much simpler. Recall that JavaScript objects are like hashtables. You can add new values at any time. If the value is a function, then it becomes a method.
So in the example above, I didn't need a Parenizor class at all. I could have simply modified my instance.
myParenizor = new Parenizor(0);
myParenizor.toString = function () {
    if (this.getValue()) {
        return this.uber('toString');
    }
    return "-0-";
};
myString = myParenizor.toString();

We added a toString method to our myParenizor instance without using any form of inheritance. We can evolve individual instances because the language is class-free.
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?
Ans:- If the radix parameter is omitted, JavaScript assumes the following:
        
  • If the string begins with "0x", the radix is 16 (hexadecimal)

  • If the string begins with "0", the radix is 8 (octal).

  • If the string begins with any other value, the radix is 10 (decimal)

  • if you try to parseInt("08") which result gives 0 because the string begins with "0" and converts next number  into Octal number."8" is not include in octal number so result gives 0.

    2)Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500?
    Ans:- Yes. As per the operator priority the * and / has first priority rather than - and +. which start from left to right. hence first 115*4 is calculate which is equal to 460. the equation is now
    460-4+88/2. now as per operator priority 88/2=44. now equation is 460-4+44=500

    3)Will the following statement evaluate to true or false
        (("red" != "car") || (3 >= 9)) && !(((10 * 2) == 100) && true) ?
    Ans:- Above function evaluate to true.
             ("red"!="car") --> TRUE. Because of || to next not evaluate next condition that is (3>=9) and return true for   (("red" != "car") ||  (3 >= 9))  statement.--i
       (20==100)--> FALSE. Because of && it gives result False to  ((10 * 2) == 100) && true)  statement. !(False)--> TRUE --ii
    so  as per i and ii TRUE &&TRUE-->Evaluate TRUE.

    Monday 9 May 2011

    Beginner's JavaScript Tutorials

                                                                         Introduction

    This course deals with Scripts. A Script is a segment of code that manipulates the browser and its contents in ways that is not possible with ordinary HTML or Cascading Style Sheets. By using a script in your web pages, you can gain more control of how the page looks and behaves: dates and times can be added to the page, form elements validated before the contents are sent, browser details checked, cookies set, even simple games can be added to a web page - all with a scripting language.

    The learning curve for scripting is a lot a steeper than HTML and Style Sheets. But you can learn the basics, and use scripts on your own pages, without it causing you too much trouble. The scripting language covered in these pages is meant to get you started on the subject, and is not intended as an in-depth study.

    We're going to study the JavaScript programming language, because it is a widely-used scripting language for web pages. All the scripts in these pages have been tested with modern versions of a wide variety of browsers. If you're ready, then, let's make a start.


    A First Script



    Let's jump right in with a bit of code. Fire up whatever HTML Editor you use (you can use our free Editor by clicking here: Download the free editor ). With your editor open, copy the following code. When you're done copying it, save your work and load it into your browser.



    <HTML>
    <HEAD>
    <TITLE>A First Script</TITLE>
    </HEAD>
    <BODY>


    <SCRIPT LANGUAGE = JavaScript>

    document.write("Hello World")

    </SCRIPT>

    </BODY>
    </HTML>



    All right, how did you get on? All that typing should have gotten you this in the browser:

    "Hello World"

    Granted, that's a heck of a lot of trouble to go to just to write "Hello World". But it's a start. Let's explain what's going on.

    When you're writing your scripts, you enclose them between two <SCRIPT> tags, an opening one and a closing one. The opening one should tell the browser what language the script is written in:

    SCRIPT LANGUAGE = JavaScript>
    The closing Script tag is just the word SCRIPT in between two angle brackets with a forward slash:

    </SCRIPT>

    Most of your JavaScript will go between these two tags. So what's all that "document dot write" bit?

    document.write("Hello World")


    Document is part of something called the Document Object Model. Document refers to all the text and HTML elements between the two BODY tags. And that includes any attributes inside the BODY tag itself. Like BGCOLOR.

    Write( ) is a method of Document. A method is a bit of code that actually does something. As opposed to a Property, which IS something. Methods are usually Verbs, and Properties usually Nouns. The Write( ) method writes text (and numbers as well) between the two BODY tags on your page.

    For all you English language experts out there who might be protesting about the lack of capital letters, Document is spelt with a lowercase "d", and Write with a lowercase "w". Try changing your code to this and see what happens:

    Document.Write("Hello World")

    JavaScript is damned picky about capital letters - it doesn't like them at all!

    The part or parts between the two brackets of write( ) are what will be written to your page. Direct text goes between two double quotes; Variables don't need any. Whoops, we haven't done variables yet. We'll get to them.

    So the whole line reads "Write the text Hello World between the two BODY tags of the web page."


    Don't worry if you don't understand some of that - the main point is that you are up and running, and you've written your first script. The journey has just started.