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:
parent
905b16289c
commit
c81d766bae
17
challenges/cats-childrens-of-childrens/README.md
Normal file
17
challenges/cats-childrens-of-childrens/README.md
Normal 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.
|
79
challenges/cats-childrens-of-childrens/input-output.json
Normal file
79
challenges/cats-childrens-of-childrens/input-output.json
Normal 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"
|
||||
]
|
||||
}
|
||||
]
|
@ -0,0 +1,5 @@
|
||||
# javascript-recursive - cats-childrens-of-childrens
|
||||
|
||||
Programming language : JavaScript
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 12 September 2020.
|
@ -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
|
Loading…
Reference in New Issue
Block a user