Friday, March 22, 2013

My solution to chapter 9 exercises of JavaScript Step by Step 2e

 This is my solution to Chapter 9 programming exercises of JavaScript Step by Step 2e.
 
1. Use the availHeight and availWidth methods to determine whether a screen is at least 768 pixels high and 1024 pixels wide. If it’s not, display an alert() dialog box stating the size of the available screen.

JavaScript:
if (screen.availHeight < 768 && screen.availWidth < 1024) {
    alert("Available height: " + screen.availHeight + "\n"
        + " Available width: " + screen.availWidth);
}

TypeScript: same as above

2. Alter the step-by-step exercise that used the location object to display an alert() dialog box based on the values of the query string. Specifically, display the word “Obrigado” if the country is specified as Brazil, and display “Thank you” if the country is Great Britain. Test these conditions.

JavaScript:
if (location.search) {
    var querystring = decodeURI(location.search).substring(1);
    var splits = querystring.split('&');
    for (var i = 0; i < splits.length; i++) {
        var splitpair = splits[i].split('=');
        if (splitpair[0].toLowerCase() === 'country') {
            if (splitpair[1].toLowerCase() === 'brazil') {
                alert('Obrigado');
            }
            else if (splitpair[1].toLowerCase() === 'great britain') {
                alert('Thanks you');
            }
        }
    }
} 
 
TypeScript:
if (location.search) {
    var querystring: string = decodeURI(location.search).substring(1);
    var splits: string[] = querystring.split('&');
    for (var i = 0; i < splits.length; i++) {
        var splitpair: string[] = splits[i].split('=');
        if (splitpair[0].toLowerCase() === 'country') {
            if (splitpair[1].toLowerCase() === 'brazil') {
                alert('Obrigado');
            }
            else if (splitpair[1].toLowerCase() === 'great britain') {
                alert('Thanks you');
            }
        }
    }
}