[Javascript] Intro to Recursion

Recursion is a technique well suited to certain types of tasks. In this first lesson we’ll look at solving a problem that requires the flattening of arrays without using recursion. Showing the shortcoming of a non-recursive solution first will help you to understand why it’s so valuable and why sometimes it's the only solution to many problem.

 

let input, config, tasks;

input = ['dist'];

config = {
  "dist": ["build", "deploy"],
  "build": ['js', 'css', 'vender'],
  "js": ['babel', 'ng-Annotate', "uglify"],
  "css": ["sass", "css-min"]
};

tasks = [];

getTasks(input);

function getTasks(input){
  
  input.forEach((task)=>{
    if(config[task]){
      getTasks(config[task]);
    }else{
      tasks.push(task);
    }
  })
};

console.log(tasks);

 

["babel", "ng-Annotate", "uglify", "sass", "css-min", "vender", "deploy"]

 

posted @ 2015-11-24 02:49  Zhentiw  阅读(277)  评论(0编辑  收藏  举报