Skip to content

Instantly share code, notes, and snippets.

@yefremov
Last active March 11, 2017 09:48
Show Gist options
  • Select an option

  • Save yefremov/b8069e2f4c3f9cc57e1d53d5398c80de to your computer and use it in GitHub Desktop.

Select an option

Save yefremov/b8069e2f4c3f9cc57e1d53d5398c80de to your computer and use it in GitHub Desktop.

Revisions

  1. yefremov revised this gist Mar 11, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nordic.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    //
    // https://www.youtube.com/watch?v=2jp8N6Ha7tY
    //
  2. yefremov revised this gist Mar 11, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions nordic.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    //
    // https://www.youtube.com/watch?v=2jp8N6Ha7tY
    //

    // Capitalize the first letter of a string, or all words in a string.
    function capitalize(str) {
    return str.charAt(0).toUpperCase() + str.substring(1);
  3. yefremov revised this gist Mar 11, 2017. No changes.
  4. yefremov created this gist Mar 11, 2017.
    69 changes: 69 additions & 0 deletions nordic.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    // Capitalize the first letter of a string, or all words in a string.
    function capitalize(str) {
    return str.charAt(0).toUpperCase() + str.substring(1);
    }

    // Arrays

    var protuguese = [
    'galinha',
    'vaca',
    'milho'
    ];

    protuguese.map(capitalize);
    // => ["Galinha", "Vaca", "Milho"]

    // Tree

    var familyTree = {
    value: 'mattias',
    nodes: [
    {
    value: 'eva',
    nodes: [
    { value: 'ove' },
    { value: 'sonja' },
    ]
    },
    {
    value: 'maths',
    nodes: [
    { value: 'anna' },
    { value: 'gustav' },
    ]
    }
    ]
    };

    function mapTree(node, mapper) {
    return {
    value: mapper(node.value),
    nodes: node.nodes
    ? node.nodes.map(function (node) {
    return mapTree(node, mapper);
    })
    : null
    };
    }

    mapTree(familyTree, capitalize);
    // => {
    // value: 'Mattias',
    // nodes: [
    // {
    // value: 'Eva',
    // nodes: [
    // { value: 'Ove' },
    // { value: 'Sonja' },
    // ]
    // },
    // {
    // value: 'Maths',
    // nodes: [
    // { value: 'Anna' },
    // { value: 'Gustav' },
    // ]
    // }
    // ]
    // };