PracticeFunctionsExercises

From GIS CS4
Jump to: navigation, search

Contents

Practice exercises for functions

For each of the exercises below

  • Write a JavaScript function.
  • Write a test function to confirm that each function works.

1. isEven

Write a JavaScript function isEven that takes as input the parameter zInt (an integer) and returns true if number is even, and returns false if number is odd.

Hint: Use the modulus operator % where (a % b) gives the remainder after dividing a by b,

e.g. 10 % 3 results in 1, the remainder of 10/3.

i.e. An even number when divided by 2 will have the remainder 0.

if ((iNum % 2) == 0) // means that iNum is exactly divisible by 2 i.e. iNum is even.

http://www.w3schools.com/js/js_operators.asp

2. isLunchtime

The function isLunchtime that takes as input the parameters ziHour (an integer in the range 1-12) and bAM (a Boolean “flag” that represents whether the hour is before noon). The function should return true when the input corresponds to 11am or 12pm (noon) and false otherwise.

3. isLeapYear

Write a JavaScript function isLeapYear that takes as input the parameter ziYear and returns true if year (an integer) is a leap year according to the Gregorian calendar and false otherwise. The Wikipedia entry for leap years contains a simple algorithmic rule for determining whether a year is a leap year. Your main task will be to translate this rule into JavaScript:

if year is divisible by 400 then is_leap_year
else if year is divisible by 100 then not_leap_year
else if year is divisible by 4 then is_leap_year
else not_leap_year

Hint: Use modulus operator 
if ((iNum%iDivisor) == 0)  // means that iNum is exactly divisible by iDivisor

4. nameAndAge

Write a JavaScript function nameAndAge that take as input the parameters zstrName (a string) and ziAge (a number) and returns a string of the form "zstrName is ziAge years old." The function should include an error check for the case when age is less than zero. In this case, the function should return the string "Error: Invalid age".

The test function for this exercise is a little tricky therefore there is some sample code given below.

The function str.indexOf(strSearch) is used to find the occurrence of the name and age text.

var testNameAndAge = function(zstrName, ziAge)
{
    var strRes;
    strRes = nameAndAge(zstrName, ziAge);

    // check if invalid age is reported
    if (ziAge < 0)
    {
        return (strRes === "Error: Invalid age");
    }

    // check that result string commences with zstrName
    if (strRes.indexOf(zstrName) !== 0)
    {
        return false;
    }

    // check that age string occurs in correct position
    // String format: "nnnn is aaa years old."
    var strAge = "" + ziAge;
    if (strRes.indexOf(strAge) !== (zstrName.length + 4))
    {
        return false;
    }

    return true;
};

var autoTestNameAndAge = function()
{
    if (testNameAndAge("Fred", -1))         {return false;}
    if (!testNameAndAge("Jess", 16))        {return false;}
    // more tests here...
    
    return true;
};

5. average

Write a JavaScript function average that take as input the parameters zInt1 and zInt2 (2 integers) and returns the average of the 2 numbers.

6. isCool

Write a JavaScript function isCool that takes as input the parameter zstrName (a string) and returns true if the name matches someone in CS4 (student or staff!) and returns false otherwise.

// use === operator to compare strings (note this is case sensitive therefore compare exact case)
var isCool = function(zstrName)
{
    if (zstrName === "Adrian")         {return true;}
    if (zstrName === "Jenny")          {return true;}

    return false;      // sorry - not cool!
};

7. nameLookup

Write a JavaScript function nameLookup that takes a string zstrFirstName that corresponds to one of the students in CS4, and then returns their corresponding last name. If zstrFirstName doesn't match any of those strings, return the string "Error: Not a CS4 student".

Hint: Use Arrays. An array is like a list. You can index an item in an array in a similar way to indexing a character in a string. The nth item is item[n-1]

var nameLookup = function(zstrFirstName)
{
    // an array (list) containing 3 strings
    var firstNameList = ["Fred",   "Joe",  "Dave"];    
    var lastNameList  = ["Bloggs", "Cool", "Smith"]; 

    // index through each name in the list
    for (var i=0; i<firstNameList.length; i++)
    {
        if (zstrFirstName === firstNameList[i])
        {
           // match first name at index i
           // return last name 
        }
    }
    // no match found - return error
};
Personal tools
Namespaces

Variants
Actions
Navigation
Tools