Monday, January 30, 2012

Socket.Io

Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It's care-free realtime 100% in JavaScript.

We can create realtime application with the use of socket.io . i have used this with my node.js server to create my app . In simple language what socket.io do . I am taking a simple daily life exmple to explain . 

Example - what is socket.io  ?
suppose you are traveling in the bus from New Delhi to Noida . you just board on bus after travelling little you ask conductor is Noida come ? . conductor say no  . after some time you again ask , is noida come ?
conductor said no . and you keep asking to conductor like this . and conductor obviously annoyed at some time .and now imagine if everyone on bus keep asking for their respective stations. this is pretty obvious he will annoy and shout . if same thing happen with server, it may crash. So what we need . that a person who would board on the bus tell conductor in advance that whenever bus arrive to Noida station just tell me . whenever that station come , conductor give a message to all, who so ever is interested just step down from bus. simple.
Same thing we want with sever , whenever a client send request to server it make a bidirectional connection with it which will not going to close after the desired event happen. so its like a tunnel. 
e.g if client want Rss feed of some foo bar website . client just once need to connect with server, whenever server got any new feed it just broadcast to user who is interested  in that feed without opening and closing connection again again . its a persistence connection between client and server. 

why we need socket.io ?
 yes why we need socket.io , sever and client request responses also handle by ajax , but why socket.io.
because we need a persistence connection between client and server. Most of the time wasted in opening and closing connection . why we need to close connection if we want regular news feed .now it make sense . if any client server req res cycle takes 300ms to perform one job . only 100ms it take to exchange data, other 200ms used in opening and closing the connection . This much waste of time it matters on internet . 

Monday, January 16, 2012

JSON



JSON is Javascript object notation . this is also a part of my learning . bcoz Its a popular data interchange format between clients and servers. 

Json is basically a lightweight data interchange format. earlier we used to with XML and HTML and many more .But Json is lightweight , Easy to understand , language independent and self describing.

As we know that Server cannot send any object directly to client . so it need any middleware which can understandable both the side  so through Json, server send any query as a string type and on client side it can easily be parsed as Object .So basically in laymen language json is a string , which can be easily converted into object and also object to string.

lets take a example. 

1.)
{
"student": [
{ "firstName":"tom" , "lastName":"decker" }, 
{ "firstName":"varun" , "lastName":"jain" }, 
{ "firstName":"lexia" , "lastName":"woodhouse" }
]

} 

2.)
'{
"employees": [{ "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" }]}'

so you can see how both look similar but First one is object and Second one is String. 

so object to string and string to object . 

JSON.stringify(data);  - this convert into string format.

JSON.parse(data);      - this convert into javascript object . 


Friday, January 6, 2012

Create Server with Node js

We can create a full fledged SERVER in Node.js , we need HTTP server to handle all web requests and responses  and to serve web pages.

lets make a simple server,At first it seems like tough task to create server , but with node js its not 

  

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

That's it! You just wrote a working HTTP server. Let's prove it by running and testing it. First, execute your script with Node.js
require HTTP  its like include header files in c/c++.  it include the module called "http". Now In  http.createServer passing anonymous function (function with no name).  


Wednesday, January 4, 2012

Tuesday, January 3, 2012

Javascript Questions


JavaScript Interview Questions and Answers
This is an interactive multiple-choice JavaScript quiz that allows you to test your knowledge of JavaScript by answering the following questions.

1) What is the output of following JavaScript code?
 a) 44
 b) 8
 c) 4
 d) Error output

2) What is the output of following JavaScript code?
 var cst = "Scripts Software Technologies";
 var result = cst.split(" ");
 document.write(result); 
 a) Scripts
 b) S,c,i,p,t,s,S,o,f,t,w,a,r,e,T,e,c,h,n,o,l,o,g,i,e,s
 c) Scripts,Software,Technologies
 d) Scripts Software Technologies

3) Is it possible to nest functions in JavaScript?
 a) True
 b) False

4) What is the output of following JavaScript code?

 a) get code name of the browser of a visitor
 b) set code name of the browser of a visitor
 c) None of the above

5) Which of the following is true?
 a) If onKeyDown returns false, the key-press event is cancelled.
 b) If onKeyPress returns false, the key-down event is cancelled.
 c) If onKeyDown returns false, the key-up event is cancelled.
 d) If onKeyPress returns false, the key-up event is canceled.

6) Scripting language is
 a) High Level Programming language
 b) Assembly Level programming language
 c) Machine level programming language

7) Which best explains getSelection()?
 a) Returns the VALUE of a selected OPTION.
 b) Returns document.URL of the window in focus.
 c) Returns the value of cursor-selected text
 d) Returns the VALUE of a checked radio input.

8) What is the output of following JavaScript code?

 function x()
 {
 var s= "Good 100%"; 
 var pattern = /\D/g;
 var output= s.match(pattern);
 document.write(output);
 }


 a) Good %
 b) 1,0,0
 c) G,o,o,d,%
 d) Error


9) What is the output of following JavaScript code?

 var cst="SCRIPTS SOFTWARE TECHNOLOGIES"; 
 alert(cst.charAt(cst.length-1));

 a) P
 b) E
 c) S
 d) Error


10) Choose the client-side JavaScript object:
 a) Database
 b) Cursor
 c) Client
 d) FileUpLoad

11) Are Java and JavaScript the same?
 a) No
 b) Yes

12) Syntax for creating a RegExp object:
(A) var txt=new RegExp(pattern,attributes);
 (B) var txt=/pattern/attributes;

Which of the above mentioned syntax is correct?
 a) (A) only
 b) (B) only
 c) Both (A) and (B)
 d) None of the above

13) What is the output of following JavaScript code?
 function x(z,t)
 {
 alert(x.length);
 }


a) Error
 b) 2
 c) 1
 d) 3

14) What is meant by "this" keyword in JavaScript?
 a) It refers current object
 b) It referes previous object
 c) It is variable which contains value
 d) None of the above

15) In JavaScript, Window.prompt() method returns true or false value?
 a) False
 b) True
 c) None of above

16) Math. round(-20.51) = ?
 b) -21
 c) 19
 d) None

17) What is the output of following JavaScript code?
 function x()
 {
 var s = "Quality 100%!{[!!";
 var pattern = /\w/g;
 var output = s.match(pattern);
 document.write(output);
 }
 a) %,!,{,[,!,!
 b) Q,u,a,l,i,t,y,1,0,0
 c) Quality 100
 d) Error

18) What is the output of following JavaScript code?
 var dev = new Array();
 dev[0] = "Web";
 dev[1] = "Development"
 dev[2] = "Testing"
 dev[3] = "javascript rocks";
 document.write(dev[0,1,2,3]);

 a) Error
 b) javascript rocks
 c) Web Development
 d) Web, Development,Testing,javascript rocks

19) Choose the server-side JavaScript object among the following:
 a) FileUpLoad
 b) Function
 c) File
 d) Date

20) What does parseFloat(9+10) evaluates to in JavaScript?
 a) 19
 b) 910
 c) None

21) What is the output of following JavaScript code?
 function x()
 {
 document.write(2+5+"8");
 }
 a) 258
 b) Error
 c) 7
 d) 78

22) _________ keyword is used to declare variables in javascript.
 a) Var
 b) Dim
 c) String

23) In JavaScript, which of the following method is used to evaluate the regular expression?
 a) eval(2*(3+5))
 b) evaluate(2*(3+5))
 c) evalu(2*(3+5))
 d) None of the above

24) What is the output of following JavaScript code?


 function x()
 {
 var s= "quality 100%"; 
 var pattern = /\d/g;
 var output= s.match(pattern);
 document.write(output);
 }

 a) 100
 b) 1,0,0
 c) q,u,a,l,i,t,y,%
 d) Error

25) What is the output of following JavaScript code?
 var cst=((45%2)==0)?"hello":"bye";
 document.write(cst);

 a) hello
 b) bye
 c) Error in string handling
 d) None of the above

26) What is the output of following JavaScript code?
 function x()
 {
 var dev = "Scripts Software Technologies";
 var pattern = new RegExp("SOFTWARE","i");
 document.write(dev.match(pattern));
 }

 a) Error
 b) SOFTWARE
 c) Software
 d) null 

27) How do you create a new object in JavaScript?
 a) var obj = {};
 b) var obj = Object();
 c) var obj=new {};
 d) None of the above

28) What does isNaN function do in JavaScript?
 a) Return true if the argument is not a number.
 b) Return false if the argument is not a number.
 c) Return true if the argument is a number.
 d) None of the above

29) If x=103 & y=9 then x%=y , what is the value of x after executing x%=y?
 a) 4
 b) 3
 c) 2
 d) 5

30) Choose the external object among the follwing:
 a) Date
 b) Option
 c) Layer
 d) Checkbox

31) Choose the four symbol pairs that represent RegExp properties lastMatch, lastParent, leftContext, and rightContext, respectively:
 a) $&, $+, $`, $'
 b) $+, $&, $', $`
 c) $&, $~, $`, $'
 d) $+, $&, $`, $'

32) Which of the following properties hold the values of the pixels of the length of the width and height of the screen resolution?
 a) screen.width and screen.height
 b) Resolution.width and Resolution.height
 c) screen.pixels.width and screen.pixels.height
 d) ViewerScreen.width and ViewerScreen.height

33) What does ParseInt("15",10) evaluates to?
 a) 15
 b) 10
 c) 151
 d) 150

34) Which JavaScript feature uses JAR files?
 a) Object signing
 b) Style sheets
 c) Netcaster channels
 d) Image rollovers

35) How to assign a function to a variable with the JavaScript Function contructor?
 a) var f=Function("x","y","return x+y");
 b) var f=Function(x,y){ return x+y;}
 c) var f= new Function("x", "y", "return x + y");

36) In JavaScript, Window.alert() is used to allow user to enter something
 a) True
 b) False
 c) None of above

37) What is the output of following JavaScript code?

function x()
 {
 var cst = "We are fast growing Software Company located in Mumbai , India.";
 var pattern = new RegExp("in","gi");
document.write(pattern.exec(cst) + " ");
 document.write(pattern.exec(cst) + " ");
 document.write(pattern.exec(cst) + " ");
 }
 a) in in In
 b) in in in
 c) in in null
 d) in null null

38) Does JavaScript has any DATE data type?
 a) Yes
 b) No

39) Math.round(-20.5)=?
 a) -21
 b) 20
 c) -20
 d) 21

40) ?_name is it valid javascript identifier?
 a) Yes
 b) No

41) What is the output of following JavaScript code?

 a) first
 b) undefined
 c) First
 d) Error

42) Which of these comment lines are used in JavaScript?
(1) // , /* ...... **/
 (2) / , /** ......./ , /*
 (3) /*......*/ , //
 (4) \*......*\ , //
 a) Only (4)
 b) Only (3)
 c) Either (3) or (4)
 d) Only (2)

43) What is the output of following JavaScript code?
 function x()
 {
 var s = "Give 100%!{[!!";
 var pattern = /\W/g;
 var output = s.match(pattern);
 document.write(output);
}
 a) ,%,!,{,[,!,!
 b) G,i,v,e,1,0,0
 c) Give 100
 d) Error

44) Which best describes void?
a) A method
 b) A function
 c) A statement
 d) An operator

45) What is the output of following JavaScript code?


 var cst="Chiasa Software Technologies";
 var result =cst.lastIndexOf("a");
 document.write(result);
 a) 2
 b) 12
 c) 11
 d) 13

46) What is the output of following JavaScript code?


 function x()
 {
 var cst = "First come, first served"; 
 var pattern = /first/g;
 document.write(cst.match(pattern)[1]);
 } 
a) first
 b) First
 c) undefined
 d) None of the above

47) What is the output of following JavaScript code?

If you call the function callme(), what will happen?

 function sum(x)
 {
 function add(y)
 {
 return x+y;
 }
 return add;
}
 function callme()
 {
 result=sum(5)(5);
 alert(result);
 }
 a) 10
 b) Error in calling Function
 c) 5
 d) None of the above

48) Who invented the JavaScript programming language?
a) Tennis Ritchie
 b) James Gosling
 c) Brendan Eich

49) Can you write HTML tags inside the JavaScript code as shown below?


 document.write("This is a heading");
 document.write("This is a paragraph.");
 document.write("This is another paragraph.");
 a) No
 b) Yes
 c) Impossible

50) Which feature is supported in MSIE 3.x?
a) split()
 b) document.clear()
 c) join()
 d) charAt()

51) How to speicfy the color of the hypertext links with JavaScript?
a) document.linkColor="#00FF00";
 b) document.LColor="#00FF00";
 c) document.LinkC="#00FF00";
 d) document.hyperTextLink="#00FF00":

52) What will be the output of following JavaScript code?
 a) Position:-7
 b) Position-1
 c) null
 d) error

53) ________ method returns the number of milliseconds in a date string.
a) setHours()
 b) setMinutes()
 c) parse()

54) ________ converts a string to floating point numbers.
a) eval
 b) ParseInt
 c) ParseFloat
 d) None

55) ________ attempts to evaluate a string representing any javascript literals or variables, converting it to a number.
a) eval
 b) parseFloat
 c) parseInt
 d) None

56) Which is not an attribute of the cookie property?
a) path
 b) host
 c) secure
 d) domain

57) How do substring() and substr() differ?
a) One is not a method of the String object.
 b) substr() takes three arguments, substring() only two.
 c) Only one accepts a desired string length as an argument.
 d) Besides the spelling, nothing.

58) Which is not a reserved word?
a) interface
 b) short
 c) program
 d) throws

59) In Javascript, which of the following method is used to find out the character at a position in a string?
a) charAt()
 b) CharacterAt()
 c) CharPos()
 d) characAt()

60) What will be the output of following JavaScript code snippter?

 var cst = "PHPMB Knowledge Base Software";
 var result =cst.substring(7,8);
 document.write(result);

a) PHPMB Kn
 b) Kn
 c) ow
 d) n

61) How do you delete an element from an options array?
a) Set it to false.
 b) Set it to null.
 c) Set it to undefined.
 d) Set it to -1

62) Is JavaScript case sensitive language?
a) Yes
 b) No

63) JavaScript RegExp Object has modifier 'i' to __________
a) Perform case-sensitive matching
 b) Perform case-insensitive matching
 c) Perform both case-sensitive&case-insensitive matching

64) What are the following looping structures are available in JavaScript?
a) for, foreach
 b) foreach, whileloop
 c) do-while loop, foreach
 d) for, while loop

65) Which of these is not a method of the Math object?
a) atan()
 b) atan2()
 c) eval()
 d) acos()

66) What will be the output of following JavaScript code snippter?

 var s = "9123456 or 80000?";
 var pattern = /\d{4}/;
 var output = s.match(pattern);
 document.write(output);
 a) 9123
 b) 91234
 c) 80000
 d) None of the above

67) In javascript, RegExp Object Method test() is used to search a string and returns _________
a) true or false
 b) found value
 c) index
 d) None of the above

68) What property would you use to redirect a visitor to another page?
a) document.URL
 b) window.location.href
 c) document.location.href
 d) link.href

69) In JavaScript, which of the statements below can be used for string declaration?
1) var dev = "javascript is awesme language ";
 2) var cst=new String("javascript is awesme language");
a) Either (1) or (2)
 b) Only (1)
 c) Neither (1) nor (2)
 d) Only (2)

70) What will be the output of following copde snippet?


 var cst = "Scripts Software Technologies";
 var result = cst.indexOf("Tech");
 document.write(result);

a) 15
 b) 16
 c) 19
 d) 17

71) What will be the output of following copde snippet?

 function x()

 {
var s = "Eat to live, but do not live to eat";
 var pattern = new RegExp("eat$");
 document.write(pattern.exec(s));
 }
 a) Eat
 b) eat
 c) undefined
 d) Eat eat

72) What will be the output of following copde snippet?

a) in in In
 b) in in in
 c) in in null
 d) in null null

73) -________ method is used to remove focus from the specified object.
a) blur()
 b) focus()
 c) None

74) What does parseFloat("FF2") evaluates to?
a) 152
 b) FF2
 c) NaN
 d) None

75) eval((20*4)=?
a) Nan
 b) 204
 c) 24
 d) 80

76) What will be the output of following JavaScript code?
a) null
 b) Base
 c) BASE
 d) Error

77) JavaScript is a ________ typed language.
a) tightly
 b) loosely




Answers of JavaScript Interview Quiz Questions
(1) a (2) c (3) a (4) a (5) a (6) a (7) c (8) c (9) c (10) d
 (11) a (12) c (13) b (14) a (15) a (16) b (17) b (18) b (19) c (20) c
 (21) d (22) a (23) a (24) b (25) b (26) d (27) a (28) a (29) a (30) d
 (31) a (32) a (33) a (34) a (35) c (36) b (37) a (38) b (39) c (40) b
 (41) a (42) b (43) a (44) d (45) b (46) c (47) a (48) c (49) b (50) d
 (51) a (52) b (53) c (54) c (55) a (56) b (57) c (58) c (59) a (60) d
 (61) b (62) a (63) b (64) d (65) c (66) a (67) a (68) b (69) a (70) b
 (71) b (72) c (73) a (74) c (75) d (76) b (77) b

Global Variables

why not to use global variable in javascript ?

JavaScript is not good in dealing with global variables .Global variable is a variable that is visible in every scope. javascript does not support Classes like  C /C++ , But we can use functions to encapsualte data.But use of global variable in a large project is not a good programming practice. it can be easily vulnerable for any large project. because being dependent on global variable degrade the reliability of the program . and can easily access to make changes in whole program . so generally we  avoid the use of global variable in javascript in large projects , it may b use in small programs .

Sunday, January 1, 2012

string reverse

Linked List

Binary Sort


arr = new Array()
arr[0] = 'Nano';
arr[1] = 'Indica';
arr[2] = 'BMW';
arr[3] = 'Innova';
arr[4] = 'Toyoto';
arr[5] = 'Alto';
arr[6] = 'Maruti';
arr[7] = 'Spark';
arr[8] = 'Chevrolet spark';

function binarySearch(arr, key){
var left = 0;
var right = arr.length - 1;
while (left <= right){
var mid = parseInt((left + right)/2);
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return arr.length;
}
function search(){
var element= binarySearch(arr,'Innova');



document.write("The element you are searching is at the index number: "+ element);
}