Created
September 9, 2020 02:02
-
-
Save landonconover/225836f0d350fa2b1124a7102ac30071 to your computer and use it in GitHub Desktop.
Company Recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let company = { | |
| sales: [{ | |
| name: 'John', | |
| salary: 1000 | |
| }, { | |
| name: 'Alice', | |
| salary: 1600 | |
| }], | |
| development: { | |
| sites: [{ | |
| name: 'Peter', | |
| salary: 2000 | |
| }, { | |
| name: 'Alex', | |
| salary: 1800 | |
| }], | |
| internals: [{ | |
| name: 'Jack', | |
| salary: 1300 | |
| }] | |
| } | |
| }; | |
| function sumSalaries(department) { | |
| //check for array (departement) | |
| if(Array.isArray(department)) { | |
| return department.reduce((prev, curr) => prev + curr.salary, 0) | |
| } else { //check for object (multi departmetns) | |
| let sum = 0; | |
| for (let subdep of Object.values(department)) { | |
| sum | |
| sum += sumSalaries(subdep); | |
| } | |
| return sum; | |
| } | |
| } | |
| sumSalaries(company) //? | |
| function filterToNum (num) { | |
| return function(x) { | |
| return x == num; | |
| } | |
| } | |
| let arr = [1,2,3,4,5]; | |
| arr.filter(filterToNum(3)) //? | |
| arr.filter(filterToNum(2)) //? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment