Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger


Sunday 14 September 2008

Javascript Interview Questions and Answers 11

How to convert a string to a number using JavaScript?
You can use the parseInt() and parseFloat() methods. Notice that extra letters following a valid number are ignored, which is kinda wierd but convenient at times.
parseInt("100") ==> 100
parseFloat("98.6") ==> 98.6
parseFloat("98.6 is a common temperature.") ==> 98.6
parseInt("aa") ==> Nan //Not a Number
parseInt("aa",16) ==> 170 //you can supply a radix or base

How to convert numbers to strings using JavaScript?
You can prepend the number with an empty string
var mystring = ""+myinteger;
or
var mystring = myinteger.toString();
You can specify a base for the conversion,
var myinteger = 14;
var mystring = myinteger.toString(16);

mystring will be "e".

How to test for bad numbers using JavaScript?
the global method, "isNaN()" can tell if a number has gone bad.
var temperature = parseFloat(myTemperatureWidget.value);
if(!isNaN(temperature)) {
alert("Please enter a valid temperature.");
}

What's Math Constants and Functions using JavaScript?
The Math object contains useful constants such as Math.PI, Math.E
Math also has a zillion helpful functions.
Math.abs(value); //absolute value
Math.max(value1, value2); //find the largest
Math.random() //generate a decimal number between 0 and 1
Math.floor(Math.random()*101) //generate a decimal number between 0 and 100

What's the Date object using JavaScript?
Time inside a date object is stored as milliseconds since Jan 1, 1970.
new Date(06,01,02) // produces "Fri Feb 02 1906 00:00:00 GMT-0600 (Central Standard Time)"
new Date(06,01,02).toLocaleString() // produces "Friday, February 02, 1906 00:00:00"
new Date(06,01,02) - new Date(06,01,01) // produces "86400000"

What does the delete operator do?
The delete operator is used to delete all the variables and objects used in the program ,but it does not delete variables declared with var keyword.

How tp create Arrays using JavaScript ?


This produces

first day is Sunday

A more compact way of creating an array is the literal notation:

This produces
first day is Sunday

How to delete an entry using JavaScript?
The "delete" operator removes an array element, but oddly does not change the size of the array.

This produces
Number of days:7
Number of days:7

No comments:

Post a Comment