From GIS CS4
Jump to: navigation, search

Feedback from Functions Exercises


  • <Scope>,<Name>,<Parameters>,<ReturnType>
  • Naming conventions –
    • use lowercase first letter of functions and variables
    • use z as first letter of function parameters
    • use prefix to indicate datatype (e.g. b=Boolean, i=integer str=string, ch=char)
  • Read the function specification!
  • Don’t mix return types e.g. in nameAndAge() return string.
  • Check return types:
return text(str, x, y); // wrong: you are not displaying text!                 
return (zstrName + " is " + ziAge + " yrs old"); //return the string

Autotest functions

 if (nameLookup(“Chip”) ===  (“Oliver”)) return false;  // There are too many cases to meaningfully test.
 if (nameLookup(“Chip”) !==  (“Rowe”)) return false;   // This is sufficient because anything other than “Rowe” is invalid.
if((average(3,5)===(-7)))   //not meaningful
if((average(3,5)!==(4)))     //meaningful – 4 is the only valid answer
  • If you can use more compact format it helps readability:
if(!isEven(2))         {return false;}    
// A function to test different use cases of the function isEven
var autotestIsEven = function()
    // test even numbers (isEven should return true)
    if (!isEven(2))   {return false;}  
    if (!isEven(0))   {return false;}  
    if (!isEven(-6))  {return false;}  

    // test odd numbers (isEven should return false)
    if (isEven(3))    {return false;}  
    if (isEven(-5))   {return false;}
   return true;    // no problems - all tests ran successfully


  • In nameLookup, include a test of a name that you don’t expect to find

Model Code

Model Code