1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-10-29 22:17:23 +01:00

feat: add cats-childrens-of-childrens challenge

This commit is contained in:
Divlo 2020-09-12 22:03:17 +00:00
parent 905b16289c
commit c81d766bae
5 changed files with 113 additions and 0 deletions

View File

@ -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.

View File

@ -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"
]
}
]

View File

@ -0,0 +1,5 @@
# javascript-recursive - cats-childrens-of-childrens
Programming language : JavaScript
Created by [@Divlo](https://github.com/Divlo) on 12 September 2020.

View File

@ -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