Archive for August, 2007

JavaScript – Beginning Interview Questions

Posted in JavaScript, Software, Technology, Web, Web 2.0, internet, programming on August 31, 2007 by Joey

In doing numerous technical interviews of Internet Application Developers, I’ve discovered that most of these candidates consider their JavaScript skills to be at an “expert” level or they feel their JavaScript skills are “very strong.” In the course of the interview, however, it is often discovered that the candidates only have a cursory understanding of the JavaScript programming language. Based on my experience, I have written 20 questions that someone claiming to know JavaScript should be able to answer:

What have you used JavaScript for?
Typical answers include form validations, user action confirmations, updating an Internet page dynamically and other such answers. I usually ask this question first just to get the candidate talking about JavaScript and see if they have in fact used JavaScript for something.

How do you access an element in an HTML document with JavaScript?
Give the element an ID attribute and then use JavaScript’s getElementById() function to access that element.

How do you determine how many characters are in a JavaScript string?
With the length property of the JavaScript string object.

What datatypes are supported by JavaScript?
Number, String, Undefined, null, Boolean. At a minimum, the candidate should identify Number and String.

How do you create a date in JavaScript?
With the Date object: new Date()

Where does JavaScript date object pull it’s date from?
From the client machine.

What does the isNaN function do?
Returns true is the value provided is not a numeric value and false if the number provided is a numeric value.

What is the difference between JavaScript and Java?
They are completely different languages.

How do you comment JavaScript code?
For single-line comments, begin the line with consecutive forward slashes (//). To comment a block of code, wrap it with /* code */.

What is the JavaScript concatenation operator?
The plus sign.

How do you call an external JavaScript file?
With the script tag, using the src attribute to reference the path of the JavaScript file.

How do you create a function in JavaScript?
Use the function statement.

How do you get the item selected in a dropdown select list?
With the selectedIndex property

How do you open a new window?
With window.open

How do you remove all options from a dropdown select list?
By setting the length property of the dropdown select list to 0.

Demonstrate the syntax for writing a FOR loop.
Need an answer similar to:
for (i=1;i<=LoopNumber;i++)
{
//do something
}

How do you determine the length of an array?
With the length property of the array object

How do you load a new URL in a browser?
With the location or href property of the window or document object.

What are the different usages of equal signs?
One (=) is for assignment. Two (==) is for equality. Three (===) is for strict comparison.

What is ++ used for
It is the increment operator. It adds one to a value.

How to Add an Option to a Dropdown Select Box with JavaScript

Posted in JavaScript on August 17, 2007 by Joey

To add an option to a dropdown select box with JavaScript, first create the option that will be added. Then add the option, as shown in the following example.

If this is the select list:

<select name='dropdown' id='dropdown'>
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>

This JavaScript will add an option at the end of the list with a value of 3 and a label of 3:

var objDropdown = document.getElementById('dropdown');
var objOption = new Option('3','3');
document.write(objOption.value);
objDropdown.options[objDropdown.length] = objOption;

The two arguments to the Option method are text and value. Text is what shows in the dropdown box and is the first argument. Value is what is sent with the form submission and is the second argument. Note also that the length of the dropdown box is used to put the new option at the end of the list. A value could be hard-coded here, if desired.

Related articles:
How to Remove All Options from a Dropdown Select Box with JavaScript
How to Remove an Option from a Dropdown Select Box with JavaScript

How to Remove an Option from a Dropdown Select Box with JavaScript

Posted in JavaScript on August 15, 2007 by Joey

To remove an option from a dropdown select box with JavaScript, use the JavaScript remove method. Pass it the index of the option that should be removed. The index is 0-based, so to remove the first item in the select list pass 0, to remove the second pass 1 and so forth.

If this is the select list:

<select name='dropdown' id='dropdown'>
  <option value='1'>1</option>
  <option value='2'>2</option>
  <option value='3'>3</option>
</select>

The second option can be removed as follows:

document.getElementById('dropdown').remove(1);

Related articles:
How to Remove All Options from a Dropdown Select Box with JavaScript

How to Create a Function in JavaScript

Posted in JavaScript on August 14, 2007 by Joey

A function can be created in JavaScript by using the function statement, as follows:

function CalculateTotal(intInput)
{
  var intTotal = 0;
  intTotal = intInput*2;
  return intTotal;
}

To call this function in JavaScript code, simply do this:

CalculateTotal(4);

The curly braces are required as part of the function statement.

How to Remove All Options from a Dropdown Select Box with JavaScript

Posted in JavaScript on August 12, 2007 by Joey

To remove all options from a dropdown select box with JavaScript, simply set the length property of the dropdown box to 0. For instance, if a dropdown select box is defined as follows:

<select name='Dropdown' id='Dropdown'>
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>

Use this JavaScript to remove all the options in the dropdown select box.

document.getElementById('Dropdown').length = 0;

How to Remove All Elements From a JavaScript Array

Posted in JavaScript on August 10, 2007 by Joey

To remove all the elements from a JavaScript array, simply set the array length to 0, as shown in the following example:

var aryTest = [1,2,3,4];
document.write(aryTest.length);

aryTest.length = 0;
document.write(aryTest.length);

Related articles:
JavaScript Arrays Summary and Reference
JavaScript Array length Property

JavaScript to Get Number of Days in a Month

Posted in JavaScript on August 10, 2007 by Joey

At work recently the need came up to determine the number of days in a month for a given month and year. This can be accomplished by creating a date object for the given month and year. Then do a while loop. In each iteration of the loop, set the day of the month, starting with 29, which is one more than the fewest days that can occur in a month. After setting the day, check if setting that day changed the month. If it did, then the number of days has been found. Otherwise, the loop iterates again.

For example, for September of 2007, the loop would use setDate() to set a date for September 29th. After doing this, the month would still be 8 (JavaScript dates are zero-relative). The loop would run again. setDate() would set the 30th and the month would still be 8. The loop would run again. setDate() would set the 31st. Since there is no 31st in September, setDate() actually sets October 1st, thus making the month 9. Since the month has changed, we know there are 30 days in September.

Code:

var intMonth = 7;//given month
var intYear = 2007;//given year

var dteMonth = new Date(intYear,intMonth);//
var intDaysInMonth = 28;//the fewest number of days in a month
var blnDateFound = false;//Set a variable to check on the while loop

while (!blnDateFound)
{
dteMonth.setDate(intDaysInMonth+1);//create the next possible day
var intNewMonth = dteMonth.getMonth();//new month date

if (intNewMonth != intMonth)//if the month has changed
  blnDateFound = true;
else
  intDaysInMonth++;
}
document.write(intDaysInMonth);

JavaScript to Replace Backslash

Posted in JavaScript, Regular Expressions, Software, Technology, programming on August 9, 2007 by Joey

Backslashes can be removed from a JavaScript string as follows:

var strBackslash = ‘\\backslash’;
var strReplace = strBackslash.replace(/\\/,”);
document.write(strReplace);

Related articles:
JavaScript Regular Expressions

How to Include an External JavaScript File

Posted in JavaScript on August 8, 2007 by Joey

To include an external JavaScript file in a file, use the script tag.

<script src='../filename.js'></script>

The src attribute refers to the path where the included file resides. The file specified is a file that contains JavaScript only and has no script tags.

Note that the end script tag immediately follows the beginning script tag.

Why use the script tag to include a JavaScript file instead of embedding the JavaScript directly within the HTML file? A number of advantages are listed below:

1) Allows for the separation of presentation and scripting.
2) Files can be included in an HTML page from other web servers.
3) Provides for modular and reusable code. For instance, a function can be created once, placed in the .js file and then used among multiple pages.
4) Placing JavaScript in it’s own file causes the browser to cache the JavaScript when it is used among multiple pages.

How to Determine the Operating System of the Client Machine with JavaScript

Posted in JavaScript on August 8, 2007 by Joey

To determine the operating system of the client machine on which a browser is executing JavaScript, use the appVersion property of the JavaScript navigator object, as demonstrated in the following example:

document.write(navigator.appVersion);

The information given in this property can be extrapolated, albeit in a somewhat rudimentary way, to determine whether the operating system is Windows or Macintosh, as shown below:

var strOS = navigator.appVersion;

if (strOS.toLowerCase().indexOf('win') != -1)
document.write('Windows');
else if (strOS.toLowerCase().indexOf('mac') != -1)
document.write('Macintosh');

Related articles:
JavaScript String Object indexOf Method