From c81d766bae6d73a0c921a3f2c6f67e7321fe4345 Mon Sep 17 00:00:00 2001 From: Divlo Date: Sat, 12 Sep 2020 22:03:17 +0000 Subject: [PATCH] feat: add cats-childrens-of-childrens challenge --- .../cats-childrens-of-childrens/README.md | 17 ++++ .../input-output.json | 79 +++++++++++++++++++ .../solutions/.gitkeep | 0 .../solutions/javascript-recursive/README.md | 5 ++ .../javascript-recursive/solution.js | 12 +++ 5 files changed, 113 insertions(+) create mode 100644 challenges/cats-childrens-of-childrens/README.md create mode 100644 challenges/cats-childrens-of-childrens/input-output.json create mode 100644 challenges/cats-childrens-of-childrens/solutions/.gitkeep create mode 100644 challenges/cats-childrens-of-childrens/solutions/javascript-recursive/README.md create mode 100644 challenges/cats-childrens-of-childrens/solutions/javascript-recursive/solution.js diff --git a/challenges/cats-childrens-of-childrens/README.md b/challenges/cats-childrens-of-childrens/README.md new file mode 100644 index 0000000..1f86ff4 --- /dev/null +++ b/challenges/cats-childrens-of-childrens/README.md @@ -0,0 +1,17 @@ +# cats-childrens-of-childrens + +Created by [@Divlo](https://github.com/Divlo) on 12 September 2020. + +## Instructions : + +Write a function that allows you to get all the cat names in a single array. + +The order, number and levels of subfolders are frequently changed, so we want a solution that adapts to these changes. + +## Source : + +[jesuisundev.com/comprendre-la-recursivite-en-7-min](https://www.jesuisundev.com/comprendre-la-recursivite-en-7-min/) + +## Examples : + +See the `input-output.json` file for examples of input/output. diff --git a/challenges/cats-childrens-of-childrens/input-output.json b/challenges/cats-childrens-of-childrens/input-output.json new file mode 100644 index 0000000..9add79c --- /dev/null +++ b/challenges/cats-childrens-of-childrens/input-output.json @@ -0,0 +1,79 @@ +[ + { + "input": [ + [ + { + "type": "folder", + "name": "cats", + "children": [ + { + "type": "image", + "name": "Buffy" + }, + { + "type": "image", + "name": "Gizmo" + }, + { + "type": "folder", + "name": "small-cat", + "children": [ + { + "type": "image", + "name": "Fluffy" + }, + { + "type": "image", + "name": "Harry" + }, + { + "type": "folder", + "name": "black-cat", + "children": [ + { + "type": "image", + "name": "Daisy" + }, + { + "type": "image", + "name": "Toby" + } + ] + }, + { + "type": "folder", + "name": "white-cat", + "children": [ + { + "type": "image", + "name": "Minnie" + }, + { + "type": "image", + "name": "Lucy" + } + ] + } + ] + }, + { + "type": "folder", + "name": "future-cat", + "children": [] + } + ] + } + ] + ], + "output": [ + "Buffy", + "Gizmo", + "Fluffy", + "Harry", + "Daisy", + "Toby", + "Minnie", + "Lucy" + ] + } +] \ No newline at end of file diff --git a/challenges/cats-childrens-of-childrens/solutions/.gitkeep b/challenges/cats-childrens-of-childrens/solutions/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/challenges/cats-childrens-of-childrens/solutions/javascript-recursive/README.md b/challenges/cats-childrens-of-childrens/solutions/javascript-recursive/README.md new file mode 100644 index 0000000..d3063a1 --- /dev/null +++ b/challenges/cats-childrens-of-childrens/solutions/javascript-recursive/README.md @@ -0,0 +1,5 @@ +# javascript-recursive - cats-childrens-of-childrens + +Programming language : JavaScript + +Created by [@Divlo](https://github.com/Divlo) on 12 September 2020. diff --git a/challenges/cats-childrens-of-childrens/solutions/javascript-recursive/solution.js b/challenges/cats-childrens-of-childrens/solutions/javascript-recursive/solution.js new file mode 100644 index 0000000..b42911d --- /dev/null +++ b/challenges/cats-childrens-of-childrens/solutions/javascript-recursive/solution.js @@ -0,0 +1,12 @@ +function solution (folders, result = []) { + for (const folder of folders) { + if (folder.type === 'image') { + result.push(folder.name) + } else if (folder.type === 'folder') { + solution(folder.children, result) + } + } + return result +} + +module.exports = solution