Skip to content

Instantly share code, notes, and snippets.

@landonconover
Created September 9, 2020 02:02
Show Gist options
  • Select an option

  • Save landonconover/225836f0d350fa2b1124a7102ac30071 to your computer and use it in GitHub Desktop.

Select an option

Save landonconover/225836f0d350fa2b1124a7102ac30071 to your computer and use it in GitHub Desktop.
Company Recursion
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