recursive search


This should be written following a TDD process. Remember that this means: RED, GREEN, REFACTOR. The refactor part of this is very very important.

You have a data structure like this:


const DIR = "dir";
const FILE = "file";

const root = {
  name: "home",
  type: DIR,
  children: [
    {
      name: "house_cats.mov",
      type: FILE
    },
    {
      name: "movies",
      type: DIR
    },
    {
      name: "workspace",
      type: DIR
    },
    {
      name: "house_cats.mp4",
      type: FILE
    },

    {
      name: "house_cats.txt",
      type: FILE
    }
  ]
};

This represents a directory structure with files and directories. (directory == folder)

Part 1

  1. Write a function that returns a list/array of all .mov files. Call this function find_all_movs
  2. Write a function that returns a list/array of all .mp4 files. Call this function find_all_mp4s
  3. Write a function that returns a list/array of all cat videos (mp4 and mov files that have the word “cat” in their name). . Call this function find_all_cat_videos

You will notice that there is a lot of repetition in the functionality you implemented. Each of these functions need to visit every FILE in the directory structure and then check if that file matches the search criteria.

Now consider te following:


const DIR = "dir";
const FILE = "file";

const root = {
  name: "home",
  type: DIR,
  children: [
    {
      name: "house_cats.mov",
      type: FILE
    },
    {
      name: "movies",
      type: DIR,
      children: [
        {
          name: "horror",
          type: DIR,
          children: [
            {
              name: "really_awful_cats.mp4",
              type: FILE
            }
          ]
        },
        {
          name: "adventure",
          type: DIR,
          children: [
            {
              name: "adventure_cats.mov",
              type: FILE
            },
            {
              name: "adventure_dogs.mov",
              type: FILE
            }
          ]
        },
        {
          name: "tragedy",
          type: DIR,
          children: [{ name: "a sad story about cats.md", type: FILE }]
        }
      ]
    },
    {
      name: "workspace",
      type: DIR,
      children: [
        {
          name: "grumpy_cats.mp4",
          type: FILE
        }
      ]
    }
  ]
};

Directory structures usually have some depth. Directories have sub directories. Sub directories also contain stuff.

Part 2

make sure find_all_movs,find_all_mp4s,find_all_cat_videos work with nested directory structures.

HINT: you’ll be using a technique called “recursion” here. play with it a little bit. The just of it is: You can call a function from within itself. Play with this a little bit

function my_recursive_function(i){
    console.log(i) ;
    if (i > 100) // this terminates the recursion
        return
    my_recursive_function(i + 1);   // this does the recursion
}

RAW CONTENT URL