• Search form is empty!

  • 013 TypeScript - optional parameters

    http://robertdunaway.github.io

    TypeScript code kata list

    All code kata lists

    Smiley face

    013 TypeScript - optional parameters

    Duration

    10 minutes

    Brief

    Using required, optional, and default parameters. Also an example of interpolation.

    For more information

    BING/GOOGLE: “TypeScript optional default parameters interpolation”

    Instructions

    Get tutorial folder or the entire katas-typescript repo.
    Open the [before/*.sln] file and execute the kata.
    Feel free to execute this kata multiple times because repetition creates motor memory.

    Github

    Kata

    Create a function that accepts several required parameters and outputs an interpolated string to the console.


    
        // Create a function that accepts several parameters.
        // These parameters are required by default.
        var profile = function (fName: string, lName: string, age?: number
            , height?: number, weight?: number): string {
    
            // String interpolation
            console.log(`name: ${fName} ${lName} age: ${age} height: ${height} weight: ${weight}`);
    
            return '';
        }
        console.log('all parameters are required');
        profile('John', 'Smith', 35, 180, 165);
    
    


    Create a function with a couple required parameters and a few optional parameters.


    
        // Create a function where only fName and lName are required and the rest
        // are optional.
        var profileWithOptions = function (fName: string, lName: string, age?: number
            , height?: number, weight?: number): string {
    
            // String interpolation
            console.log(`name: ${fName} ${lName} age: ${age} height: ${height} weight: ${weight}`);
    
            return '';
        }
        console.log('example of optional parameters');
        profileWithOptions('John', 'Smith');
    
    


    Create a function with a couple required parameters and a few default parameters.


    
        // Create function with default parameters.
        var profileWithDefaults = function (fName: string, lName: string, age: number = 18
            , height: number = 150, weight: number = 100): string {
    
            // String interpolation
            console.log(`name: ${fName} ${lName} age: ${age} height: ${height} weight: ${weight}`);
    
            return '';
        }
        console.log('example of default parameters');
        profileWithDefaults('John', 'Smith');
        profile('John', 'Smith', 35, 180, 165);
    
    


    Smiley face

    Next

    Take a few minutes and imagine more examples.

    Smiley face

    0 comments:

    Post a Comment