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