Functions and Algorithms

    In line with M261, everything in JavaScript is in terms of a function and an algorithm to evaluate it. Consider the following simple function on Strings. SECONDCHAR returns the second character of the input string, where we can use the functions of the M261 data type String together with the function LEN. On the left is the course description of the function, it's domain and an algorithm to evaluate it. On the right is a JS function that does the same thing, also using the functions of the M261 data type string.
     
    M261 pseudo code JavaScript
    SECONDCHAR : S -> C 
                s |-> sigma
    function SECONDCHAR(s) { 
    //S -> C , a comment.
    domain LEN(s) > 1 if (LEN(s)< 2) return false  
    //the domain condition
    alg 
    sigma = FIRST(REST(s))
    return FIRST(REST(s)) 
    }

    Remarks

     Picked out in red are some of the important differences between the two approaches.
    • All JS functions start with the keyword function followed by the name and the input(s). The body of the algorithm to evaluate this function is contained between curly brackets as shown. This is the standard notation of the programming language C and its followers C++ and Java. All blocks of code are contained between curly brackets.
    • A boolean condition is placed between brackets (....). Notice that, in order to catch the invalid inputs as the JS function is evaluated, the boolean condition in the JS version of the domain condition is the compliment of the set of valid inputs defined by the condition in the pseudo code version. We could leave out the domain condition in JS provided we made sure not to try to evaluate the function with an invalid input. Sometimes it is better not to try and catch the exceptions, particularly in recursive functions.
    • Comments on a single line follow the double backslash //. JS ignores anything on the line that follows after these. We could have left the comments out but we place them here to remind us of the Syntax of the function. JS is loosely typed so we must be careful to ensure that we only input strings and that the output will be a character.
    • The output statement of M261, usually of the form sigma = ...  is replaced by return .... All JS functions must output something, even if it is just 'true'.

     In general

    M261 Pseudo code JavaScript
    NAME : Set1 -> Set2 
         input |-> output
    function  NAME(input){1 //2 Set1 -> Set2 
    domain  boolean [that defines the subset of Set1 of valid inputs.] if (boolean) return false 
    // domain condition catches invalid inputs 3
    alg  
       lines of 
       code 
       --- 
          --- 
    output = ...
       lines of  
      code, each  
      line terminated  
      by a <return> and/or 
      a ';' 
    return 4 ... 
    }1
    It is not absolutely necessary to place the domain condition in the JS function. You could just remember not to use an invalid input. However, if you use the function in a composite call of functions, then writing in  the domain condition becomes an essential part of the function algorithm. 
    Note that if D is the domain set then we can either use the boolean condition of Compliment(D), as on the right above, or use that of D in the form; 
    -------{ 
    if (boolean D) 
         {..the function body..} 
    else return false 
    }

    Remarks

    • 1 The two curly brackets define the start and the end of the function
    • 2 The double back-slash (//) marks the start of a comment. JS will ignore the rest of the line.
    • 3 This boolean (true or false) is placed in brackets (---). It excludes non valid inputs and so is the compliment to that used in the M261 domain condition.
    • 4 The M261 'output  = ...' is replaced by 'return ...'.